Struts2的图片上传,下载,打开图片Demo

web模块
		<H1>Struts2的上传</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>

<h2>下载图片</h2>

	<s:url var="downLoadUrl" namespace="/sy" action="uploadAction_download.action"></s:url>
	<s:a href="%{#downLoadUrl}">下载</s:a>

记得配置XML
public class UploadAction extends BaseAction {

	private static final long serialVersionUID = -7624204829018896692L;
	
	private File file;//变量名指的是jsp的name属性 就是你要上传的文件 xxx
	private String fileContentType;// xxxContentType
	private String fileFileName;//  xxxFileName
	
	private String serverDir = "/image";//图片储存的位置
	//上传图片
	public String upload() {
		System.out.println(fileFileName);
		System.out.println(fileContentType);
		
		//指的是Linux下的上传文件的位置
		String realPath = getRealPath(serverDir + "/" + fileFileName);
		System.out.println(realPath);
		/*
		 * 参数1:本地图片  参数2:在服务器生成的文件
		 */
		
		try {
			FileUtils.copyFile(file, new File(realPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	/**
	 * 指的是Linux下的上传文件的具体所在位置
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		return application.getRealPath(path);
	}
	
	//打开图片
	public String opens() {
		String type = "image/gif";
		String name = "3.gif";
		response.setContentType(type);
		response.setHeader("Content-Disposition","filename=" + name);//文件名
		/*
		 * 将远程的图片输出到本地
		 * 数据源inputstream:远程   new File(realPath);
		 * 目的:输出到本地的jsp
		 */
		String realPath = getRealPath(serverDir + "/" + name);
		try {
//			FileUtils.copyFile(new File(realPath), response.getOutputStream());
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(realPath)));
			BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
			copyStream(in, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	//下载图片
	public String download() {
		String type = "image/gif";
		String name = "3.gif";
		response.setContentType(type);
		response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
		/*
		 * 将远程的图片输出到本地
		 * 数据源inputstream:远程   new File(realPath);
		 * 目的:输出到本地的jsp
		 */
		String realPath = getRealPath(serverDir + "/" + name);
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void copyStream(BufferedInputStream in,BufferedOutputStream out) throws IOException {
		byte[] bbuf = new byte[1024];
		int len = 0;
		while((len = in.read(bbuf)) != -1) {
			out.write(bbuf,0,len);
		}
		in.close();
		out.close();
		
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值