struts2 实现文件下载
2017-01-22 12:25
127 查看
struts2文件下载步骤
1、创建文件下载Action类
2、struts.xml配置
3、注意事项
一、创建文件下载Action类
二、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>
<!-- 指定Web应用的默认编码,相当于调用request的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- Struts2处理的请求后缀,默认值是action -->
<constant name="struts.action.extension" value="do" />
<!-- 开发模式下使用,这样可以打印出更详细的日志信息 -->
<constant name="struts.devMode" value="false" />
<package name="ssh" namespace="/" extends="struts-default">
<action name="downloadFile" class="com.web.test.DownloadFileAction">
<result name="success" type="stream">
<!-- 文件下载类型,等价于getDownloadFileContentType()方法 -->
<param name="contentType">${downloadFileContentType}</param>
<!-- 文件下载输入流,等价于getDownloadFileInputStream()方法 -->
<param name="inputName">downloadFileInputStream</param>
<!-- 文件下载名称,等价于getDownloadFileName()方法 -->
<param name="contentDisposition">attachment;fileName="${downloadFileName}"</param>
<!-- 文件下载大小(有下载进度显示),等价于getDownloadFileLength()方法 -->
<param name="contentLength">${downloadFileLength}</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
三、注意事项
暂时没有,有问题请留言,有时间会加上去
最终效果
http://192.168.2.177:9000/ssh/downloadFile.do
1、创建文件下载Action类
2、struts.xml配置
3、注意事项
一、创建文件下载Action类
package com.web.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.activation.MimetypesFileTypeMap; /** * 文件下载控制层 * * @author ylf尘风 * */ public class DownloadFileAction { // 通用文件下载 private InputStream downloadFileInputStream; private String downloadFileName;// 下载文件名称 private String downloadFileContentType;// 下载文件类型 private Long downloadFileLength;//下载文件大小 public InputStream getDownloadFileInputStream() { return downloadFileInputStream; } public void setDownloadFileInputStream(InputStream downloadFileInputStream) { this.downloadFileInputStream = downloadFileInputStream; } public String getDownloadFileName() { return downloadFileName; } public void setDownloadFileName(String downloadFileName) { this.downloadFileName = downloadFileName; } public String getDownloadFileContentType() { return downloadFileContentType; } public void setDownloadFileContentType(String downloadFileContentType) { this.downloadFileContentType = downloadFileContentType; } public Long getDownloadFileLength() { return downloadFileLength; } public void setDownloadFileLength(Long downloadFileLength) { this.downloadFileLength = downloadFileLength; } public String execute() throws UnsupportedEncodingException { File file = new File("D:\\temp_upload\\exe4j.zip"); InputStream inputStream = null; try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } this.setDownloadFileName(java.net.URLEncoder.encode("测试.zip", "UTF-8"));// 解决中文乱码 this.setDownloadFileContentType((new MimetypesFileTypeMap()).getContentType(file));// 获取文件类型 this.setDownloadFileInputStream(inputStream); //文件大小,单位:字节byte(必须和文件实际大小相同,不然会下载失败) this.setDownloadFileLength(6148344L); return "success"; } }
二、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>
<!-- 指定Web应用的默认编码,相当于调用request的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- Struts2处理的请求后缀,默认值是action -->
<constant name="struts.action.extension" value="do" />
<!-- 开发模式下使用,这样可以打印出更详细的日志信息 -->
<constant name="struts.devMode" value="false" />
<package name="ssh" namespace="/" extends="struts-default">
<action name="downloadFile" class="com.web.test.DownloadFileAction">
<result name="success" type="stream">
<!-- 文件下载类型,等价于getDownloadFileContentType()方法 -->
<param name="contentType">${downloadFileContentType}</param>
<!-- 文件下载输入流,等价于getDownloadFileInputStream()方法 -->
<param name="inputName">downloadFileInputStream</param>
<!-- 文件下载名称,等价于getDownloadFileName()方法 -->
<param name="contentDisposition">attachment;fileName="${downloadFileName}"</param>
<!-- 文件下载大小(有下载进度显示),等价于getDownloadFileLength()方法 -->
<param name="contentLength">${downloadFileLength}</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
三、注意事项
暂时没有,有问题请留言,有时间会加上去
最终效果
http://192.168.2.177:9000/ssh/downloadFile.do
相关文章推荐
- struts2实现文件下载
- OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码)
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- Struts2 结合HttpClient 实现远程服务器文件下载
- Struts2中实现多文件上传于下载
- 利用Struts2 实现文件下载
- Struts2实现文件下载(读取数据库CLOB类型的CDA)
- struts2实现文件的上传和下载实例
- 使用Struts2实现文件的上传与动态下载(1)
- struts2 实现文件的上传与下载
- Java程序员从笨鸟到菜鸟之(七十一)细谈struts2(十三)struts2实现文件上传和下载详解
- Java程序员从笨鸟到菜鸟之(七十一)细谈struts2(十三)struts2实现文件上传和下载详解
- struts2中通过Action以InputStream的下载文件以及在iOS以及Android端接收的实现方式
- Extjs4 实现文件的下载(后台使用struts2实现)
- Struts2使用注解实现文件的上传与下载(一)
- Struts2使用注解实现文件的上传与下载(二)
- JavaWeb中struts2实现文件上传下载功能实例解析
- struts2文件下载功能实现(文件名中文转码)
- Struts2实现文件的上传和下载
- struts2实现文件下载