struts2中文件下载的注意事项
2013-04-18 10:19
357 查看
配置文件如下:
<package name="download" extends="struts-default" namespace="/">
<default-action-ref name="download"></default-action-ref>
<action name="download" class="com.inspur.action.FileDownloadAction" method="download">
<param name="inputPath">\upload\8.gif</param>
<result name="success" type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="struts.gif"</param>
<param name="bufferSize">4096</param>
</result>
<result name="login">/download.jsp</result>
</action>
</package>
使用默认的下载支持拦截器download,此次success不在是type默认的jsp文件类型而变为stream类型这样在配置stream类型的结果集中要配置四个属性:contentType文件类型,inputName下载文件的输入流接口对应的值为targetFile该属性是在action中的一个属性,此次在配置文件中获得action中的属性值,必须在actgion中提供targetFile的get/set访问器。contentDisposition下载后保存的文件的文件名。bufferSize下载的缓冲区大小。在配置文件中的inputPath是用来动态配置在action中的属性即下载文件的入口。
下载的action代码如下:
package com.inspur.action;
import java.io.InputStream;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
private String inputPath;
private InputStream targetFile;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public InputStream getTargetFile() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public void setTargetFile(InputStream targetFile){
this.targetFile=targetFile;
}
public String download(){
ActionContext act=ActionContext.getContext();
Map session=act.getSession();
String title=(String)session.get("title");
if(title!=null&&title.equals("lzhq")){
return SUCCESS;
}else{
act.put("tip", "please set the title is lzhq");
return LOGIN;
}
}
}
通常在文件的下载过程中要求有下载权限,所以在action代码中添加权限校验支持,如果下载权限不够则跳转到 input逻辑视图,并显示提示信息。
<package name="download" extends="struts-default" namespace="/">
<default-action-ref name="download"></default-action-ref>
<action name="download" class="com.inspur.action.FileDownloadAction" method="download">
<param name="inputPath">\upload\8.gif</param>
<result name="success" type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">filename="struts.gif"</param>
<param name="bufferSize">4096</param>
</result>
<result name="login">/download.jsp</result>
</action>
</package>
使用默认的下载支持拦截器download,此次success不在是type默认的jsp文件类型而变为stream类型这样在配置stream类型的结果集中要配置四个属性:contentType文件类型,inputName下载文件的输入流接口对应的值为targetFile该属性是在action中的一个属性,此次在配置文件中获得action中的属性值,必须在actgion中提供targetFile的get/set访问器。contentDisposition下载后保存的文件的文件名。bufferSize下载的缓冲区大小。在配置文件中的inputPath是用来动态配置在action中的属性即下载文件的入口。
下载的action代码如下:
package com.inspur.action;
import java.io.InputStream;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
private String inputPath;
private InputStream targetFile;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public InputStream getTargetFile() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public void setTargetFile(InputStream targetFile){
this.targetFile=targetFile;
}
public String download(){
ActionContext act=ActionContext.getContext();
Map session=act.getSession();
String title=(String)session.get("title");
if(title!=null&&title.equals("lzhq")){
return SUCCESS;
}else{
act.put("tip", "please set the title is lzhq");
return LOGIN;
}
}
}
通常在文件的下载过程中要求有下载权限,所以在action代码中添加权限校验支持,如果下载权限不够则跳转到 input逻辑视图,并显示提示信息。
相关文章推荐
- php生成csv文件并提供下载及相关注意事项
- 在官网下载Struts2注意事项:
- struts2的下载及注意事项
- 文件的上传和下载—上传的实现,注意事项
- servlet 文件下载及注意事项
- 文件的上传和下载—上传的实现,注意事项
- jsp下载文件的实现方法及注意事项
- struts2文件上传及注意事项
- PHP 下载导出中文名的文件的编码注意事项
- PHP连接SQLSERVER 注意事项(附dll文件下载)
- 动态生成 Excel 文件供浏览器下载的注意事项
- struts2文件上传注意事项
- 下载文件时候注意事项 临时文件的使用
- struts2文件下载 注意中文问题
- struts2之整合sitemesh(关于过滤一些不使用模板文件的注意事项)
- jsp下载文件的实现方法及注意事项 (转)
- 动态生成 Excel 文件供浏览器下载的注意事项
- Struts2文件上传,以及各种注意事项
- strut2文件下载及注意事项
- HttpServletResponse response(中文乱码、文件下载、定时刷新、控制缓存、重定向、注意事项)