注:struts2提供的下载功能,在action中只需要提供一个返回inputStream流的方法
实现文件下载的流程
jsp视图(form表单)->struts.xml(找到对应的action)->xxxAction.java(excute())->struts.xml(找到对应的result这里是“sucess”),通过inputName属性找到对应的InputStream方法->xxxAction调用getInputStream()方法,得到返回值
1.struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8"/> <package name="parameter" namespace="/" extends="struts-default"> <action name="download" class="com.ru.action.downloadaction"> <param name="inputPath">\document\eeeeeeeeee.docx</param> <!-- struts2实现文件下载 --> <!-- 1.result必须配制成stream类型 --> <!-- 2.要配置4个属性 --> <result name="sucess" type="stream"> <!-- 下载的文件类型 --> <param name="contentType">application/vnd.openxmlformats-officedocument.wordprocessingml.document</param> <!-- 指定getInputStream()方法返回的输入流“inputStream” --> <param name="inputName">inputStream</param> <!-- 指定下载文件名 --> <param name="contentDisposition">"文档"</param> <!-- 指定下载文件的缓冲大小 --> <param name="bufferSize">4096</param> </result> </action> </package> </struts>
2.downloadaction.java
package com.ru.action; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class downloadaction extends ActionSupport{ //struts2提供的下载功能,在action中只需要提供一个返回inpuutStream流的方法 //通过struts.xml配置文件动态指定属性值,这个属性用来指定文件位置 private String inputPath; public String getInputPath() { return inputPath; } //传递属性值的setter方法 public void setInputPath(String inputPath) { this.inputPath = inputPath; } //提供inputStream流的方法 /*注:方法名getInputStream,那么对应的struts配置文件中的inputName就必须 *写成inputStream */ public InputStream getInputStream(){ return ServletActionContext.getServletContext(). getResourceAsStream(getInputPath()); } @Override public String execute() throws Exception { return "sucess"; } }
注: 上面的getInputStream()方法只能用于相对路径,及tomcat下的路径
第二个例子,下载显示文件名称。
(1)访问连接:
function dowanloadVirusFile(virusFileName,fileName){ window.location.href="downloadFile_virusFile.action?fileName="+fileName+"&downLoadFileName="+virusFileName; }
(2)strus.xml
<action name="*_virusFile" class="virusFileAction" method="{1}"> ... 其他result ... <!-- 文件下载 --> <result name="downloadvirus" type="stream"> <!-- 下载文件类型定义 --> <param name="contentType">application/octet-stream</param> <!-- 下载文件输出流定义 的方法名称--> <param name="inputName">downloadStream</param> <!-- 下载文件处理方式,下载是显示的文件名称 --> <param name="contentDisposition">attachment;filename="${fileName}"</param> <!-- 下载文件的缓冲大小 --> <param name="bufferSize">8192</param> </result> <result name="downloadVirusFile" type="json"> <param name="includeProperties">message</param> </result> ... ... </action>
(3)action.java
private String fileName; public String downLoadFileName; public String getDownLoadFileName() { return downLoadFileName; } public void setDownLoadFileName(String downLoadFileName) { this.downLoadFileName = downLoadFileName; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } //action方法 public String downloadFile(){ ........一些处理代理....... return "downloadvirus"; } //当downloadFile()返回downloadvirus字符串后,struts开始调用getDownloadStream()方法下载文件 public InputStream getDownloadStream(){ String virusPath = path + "/" + downLoadFileName; FileInputStream inputStream = null; try { inputStream = new FileInputStream(new File(virusPath)); } catch (FileNotFoundException e) { e.printStackTrace(); } return new BufferedInputStream(inputStream); }
第三个例子,不实用strus的下载功能,即只需要链接
(1)连接:
<input type="button" class="derive" value="" onclick="location='licenseExport.action'">
(2)struts.xml
<action name="licenseExport" class="sysRunConfigAction" method="licenseExport"></action>
(3)action.java
public void licenseExport() throws Exception{ downloadFile(licensePath, "licenseCode.txt", ServletActionContext.getResponse()); } public static void downloadFile(String filePath,String fileName, javax.servlet.http.HttpServletResponse response) { // // 打开指定文件的流信息 FileInputStream fs = null; File file=null; try { file=new File(filePath); fs = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); return; } // 设置响应头和保存文件名 response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // 写出流信息 int b = 0; try { PrintWriter out = response.getWriter(); while ((b = fs.read()) != -1) { out.write(b); } fs.close(); out.close(); System.out.println("文件下载完毕!"); //下载完删除文件 if(null!=file&&filePath.indexOf("pkg")!=-1){ if(file.delete()) System.out.println("文件删除完毕!"); } } catch (Exception e) { e.printStackTrace(); System.out.println("下载文件失败!"); } }