package com.company.core.download.action; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.HashSet; import java.util.Set; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.company.core.action.BaseAction; import com.company.core.interceptors.LoginIgnore; import com.opensymphony.xwork2.ModelDriven; /** * 所下载文件相关的的几个属性:文件格式 contentType, * 获取文件名的方法inputName, * 文件内容(显示的)属性contentDisposition, * 限定下载文件 缓冲区的值bufferSize * */ @Namespace("/download") @Action("") @ParentPackage("cc") @Results({ @Result(name = "success", type = "stream", params = { "contentType", "application/octet-stream;charset=ISO8859-1", "inputName", "inputStream", "contentDisposition", "attachment;filename=\"${fileName}\"", "bufferSize", "4096" }) }) public class DownloadAction extends BaseAction implements ModelDriven { /** * 序列号 */ private static final long serialVersionUID = -8423083786146592523L; /** * 下载文件的参数名 */ private String fileName; /** * 文件名称 */ private String inputName; /** * TODO */ private static Set<String> allowFix=new HashSet<String>(); static{ allowFix.add("pdf"); allowFix.add("doc"); allowFix.add("docx"); allowFix.add("xlsx"); } /** * fdos * @param fileName todo * @return todo */ private boolean allowedFix(String fileName){ String fix = fileName.substring(fileName.lastIndexOf(".")+1); return allowFix.contains(fix); } public String getFileName() throws UnsupportedEncodingException { //解决下载文件的中文乱码问题 fileName=new String(fileName.getBytes(),"ISO-8859-1"); return fileName; } public void setFileName(String fileName) throws UnsupportedEncodingException { this.fileName = fileName; } /** * 获得inputstream * @return inputStream * @throws Exception e */ public InputStream getInputStream() throws Exception { String s = File.separator; String path = ServletActionContext.getServletContext().getRealPath("/"); String filepath = path + "demo_utils"+s+"jsp"+s+"download"+s+"files"+s+fileName; return new FileInputStream(filepath); } /* * 使用struts2注解方式进行下载, * 其中contentType,inputName,contentDisposition,bufferSize分别代表下载文件类型、指定被下载文件的入口输入流、指定下载文件的文件名、指定下载文件时的缓冲大小 * 其中params={key1,value1,key2,value2..},下面注解里面的inputName的value值代表getInputStream()方法(潜规则为去掉get前缀、首字母小写的字符串即为inputStream) * 如果想在注解里面添加字符串,或者想调用方法并获得返回值,那么需要使用\"${方法名字这里依然遵守潜规则}\"这种形式,比如在"attachment;filename=gdc.exe",可以将gdc.exe替换为\"${方法名}\"通过调用该方法获得下载文件名字 * application/octet-stream表示可以下载任何文件 */ @LoginIgnore @Action(results = { @Result(name = "success", type = "stream", params = { "contentType", "application/octet-stream;charset=utf-8", "inputName", "inputStream", "contentDisposition", "attachment;filename=\"${fileName}\"", "bufferSize", "4096" }) }) @Override public String execute() throws Exception { if(!allowedFix(fileName)){ return "downloadfail"; } return SUCCESS; } public Object getModel() { // TODO Auto-generated method stub return null; } }
<a href="<%=contextPath %>/download/!execute.action?fileName=<%=java.net.URLEncoder.encode("测试文档.xlsx","utf-8") %>" target=_blank >测试文档.xlsx</a>
注: tomcat server.xml 已配置 utf-8
相关推荐
Struts2注解详细说明文档,详细讲述struts2的注解使用
本文档对利用struts2 注解 annotation 实现文件下载作了简单介绍,并有代码为例,希望对学习struts2注解annotation的人有帮助,特别是需要动态传参方面。
注解实现的页面跳转其特点是不用配置文件struts.xml因而可以实现零配置,同时由于有注解的功能,所以说大大增加了程序的可维护性。
本资源为本人自己项目过程中的全案例,从jsp到后台,无一不详,希望能给后来者尽量多的帮助
而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定...
基于struts2的文件上传及下载,使用注解配置,内含相应的jar包,方便学习使用,
而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定...
Struts2 基于注解的文档上传与下载方法,Struts2 基于注解的文档上传与下载方法,Struts2 基于注解的文档上传与下载方法
使用struts2基于注解(零配置)实现的文件上传与下载的代码,可以正常运行
spring和struts2注解讲解说明和使用
http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接
都被它莫名其妙的错误搞的郁闷,而网上关于这方面的东西大多都是基于struts2.0版本的,对我们现在用的2.1以上的版本不起什么作用,所以特整理出一份文档,里面详细说明了怎样用注解出配置struts2的action,...
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。• 包命名习惯来指定Action位置• 命名习惯制定结果(支持JSP,FreeMarker等)路径• 类名到URL的约定转换• 包名...
struts2 使用注解现在零配置不需要在使用struts.xml配置文件。 struts2 注解实例。可以直接跑
本文档介绍了Struts2以及spring的注解配置。
使用MyEclipse和Gradle 进行Struts 2.5.14.1 注解式开发项目,总共四个压缩包: (1)struts2-anotation.war 打包发布的war可以直接发布到tomcat...(4)struts2-anotation_Gradle.zip 注解式开发的Gradle文件压缩
使用注解可以用来替换struts.xml配置文件!
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
使用的是注解的方式完成的,因为注解的方式可以减少一些配置文件,比较方便的。 【特别强调】一定要清楚如何调试项目,因为自己的环境和下载的资源的环境可能不一样,需要修改配置文件或是其他才能正常运行。...
其中SSM为Myeclipse9.0工程(工程在页面层有bug,建议用Debug模式,跟踪查看其效果) ...1、无数据库文件:各位可以根据实体文件news.java创建数据库 ...下次会上传Spring3+struts2+mybatis3的全注解方式)