注: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("下载文件失败!"); } }
相关推荐
使用struts2实现文件下载功能,可以直接运行!
Struts2,文件下载 内用JAR包。有注释。一看就明白~ 测试可用
在struts2基础上实现的包括单文件、多文件的上传及下载
用的时候自己在D:\apache\Tomcat 6.0\webapps\Filedownload下创建文件夹upload往文件夹里放一个1.xls
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载...
struts2中实现文件上传 struts2中实现CRUD struts2中的OGNL struts2的新表单标志的使用 struts2与AJAX一 struts2与AJAX二 struts2与AJAX三 struts2中用Spring实现IOC struts2中的零配置与IOC struts2介绍之使用连接...
Struts2实现文件上传的功能,简洁方便,欢迎下载。
简单的下载文件代码,下载中文附件,下载已经存在的文件方案介绍
struts实现的文件上传下载功能,最大可以上传到150MB
Struts 2中实现文件下载 文件下载
struts与hibernate实现文件的上传与动态下载 主要就是运用struts2.2和hibernate3.6进行文件上传与下载,并且能动态的下载,里面包括了详细的构建文档,以及代码
自己做的一个struts2 实现文件批量上传功能,
struts1和struts2分别实现文件上传下载功能
struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传
struts2实现的文件上传下载,上传文件重命名(按时间戳)
struts1 实现文件上传下载...
Struts2的开发文档,学习Struts2时可以用来查阅API相关接口说明,便于分析框架原理
Struts2文件流方式导出下载excel、Txt、image图片,直接返回给浏览器提示下载,不生成临时文件,直接保存在客户端