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;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43162661/article/details/83213154