struts 图片的上传下载

1,上传图片
1.1 前台显示页面:

<body>

<h1>struts图片的上传下载</h1>
<form action="${pageContext.request.contextPath }/sy/uploadAction_upload.action" enctype="multipart/form-data" method="post">
	<input type="file" name="file">
	<input type="submit" value="上传">
</form>
</body>

1.2后台方法:
String name="####";这个地址一定要和你的图片名一模一样。保存和上传的方法中要一样

需要注意的是 <s:file name=“file”/>的name是什么
File file 就是什么
String fileContentType //就是加上ContentType
String fileFileName; // 加上FileName

package com.zking.interceptor;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.zking.four.web.BaseAction;

public class UploadAction extends BaseAction{

	private File file;
	private String fileContextType;
	private String fileFileName;
	private String serverDir = "/upload";
	
	
	//上传图片
	public String upload() {
		System.out.println(file);
		System.out.println(fileContextType);
		System.out.println(fileFileName);
		System.out.println("=====");
		String realPath = getRealPath(serverDir + "/" +fileFileName);
        System.out.println(realPath);
		try {
			FileUtils.copyFile(file, new File(realPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	
	//本地的路径
	private  String getRealPath(String path) {
		return application.getRealPath(path);
	}
	
	//打开图片
	public String openAs() {
		String type = "image/jpeg";
		String name = "125E06BCDAA3AB235FE07865BF947B8A.jpg";
		
		response.setContentType(type);
		response.setHeader("Content-Disposition","filename=" + name);
		
		File serverFile = new File(getRealPath(serverDir + "/" + name));
		try {
			FileUtils.copyFile(serverFile, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	//保存
	public String saveAs() {
		String type = "image/jpeg";
		String name = "125E06BCDAA3AB235FE07865BF947B8A.jpg.jpg";
		response.setContentType(type);
		response.setHeader("Content-Disposition","attachment;filename=" + name);//
		
		File serverFile = new File(getRealPath(serverDir + "/" + name));
		try {
			FileUtils.copyFile(serverFile, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContextType() {
		return fileContextType;
	}
	public void setFileContextType(String fileContextType) {
		this.fileContextType = fileContextType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	
}

前台页面

<body>
<h2>直接展示</h2>
<s:url var="openAsUrl" namespace="/sy" action="uploadAction_openAs"></s:url>

<img alt="" src='<s:property value="#openAsUrl"/>'>
<h2>保存图片</h2>
<s:url var="saveAsUrl" namespace="/sy" action="uploadAction_saveAs"></s:url>
<s:a href="%{#saveAsUrl}">保存图片</s:a>
<s:debug/>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值