struts2 实现文件上传下载 (下载支持中文文件名)代码
struts2 实现文件上传:
Action 代码:
package com.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends BaseAction{ /** * */ private static final long serialVersionUID = 1L; /** * 上传后存放在临时文件夹里的文件 */ private File <span style="color:#ff0000;">file;</span> /** * 文件名称 */ private String <span style="color:#ff0000;">fileFileName</span>; /** * 文件的MIME类型 */ private String<span style="color:#ff0000;"> fileContentType</span>; /** * 保存的路径 */ private String savePath; public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String execute() throws Exception { String root = ServletActionContext.getServletContext().getRealPath(savePath); InputStream is = new FileInputStream(file); OutputStream os = new FileOutputStream(new File(root, fileFileName)); byte[] buffer = new byte[500]; @SuppressWarnings("unused") int length = 0; while(-1 != (length = is.read(buffer, 0, buffer.length))) { os.write(buffer); } os.close(); is.close(); return SUCCESS; } }
struts2.xml配置信息:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="fileUpload" extends="struts-default" namespace="/"> <action name="fileUpload_*" class="com.action.FileUploadAction" method="{1}"> <param name="savePath">/upload</param> //注意在当前工程里建一个 upload文件夹 <result> /login.jsp </result> </action> </package> </struts>
文件上传的 jsp 页面:
<form action="fileUpload.action" method="post" enctype="multipart/form-data"> file: <input type="file" name="file"/><br> <input type="submit" value="submit"/> </form>
struts2实现文件下载源码:支持中文文件名
action 的代码:
package com.action; import java.io.IOException; import java.io.InputStream; import org.apache.struts2.ServletActionContext; public class FileDownloadAction extends BaseAction{ /** * */ private static final long serialVersionUID = 1L; /** * 文件名称 */ private String fileName; public String getFileName() throws IOException { <span style="color:#ff0000;">return new String(fileName.getBytes(), "ISO8859-1"); //转码 使其支持中文,不然无法显示文件名的中文字符</span> } public void setFileName(String fileName) { this.fileName = fileName;; } public InputStream getInputStream() throws Exception{ return ServletActionContext.getServletContext().getResourceAsStream( fileName); } public String execute()throws Exception{ return SUCCESS; } }
下载的 struts2.xml配置信息:
<action name="download" class="com.action.FileDownloadAction"> <result type="stream" name="success"> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <param name="inputName"><span style="color:#ff0000;">InputStream</span></param> //InputStream 是有Action中的<span style="font-family: Arial; font-size: 14px; line-height: 26px;">getInputStream()方法 去掉get而定的,要一致</span> <param name="bufferSize">10485760</param>//文件大小(10M) </result> </action>
jsp页面:
<a href="download.action?fileName=upload/铭记11er.mp3">点击下载</a>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。