struts2文件下载和文件上传

来源:互联网 发布:Linux安装telnet yum 编辑:程序博客网 时间:2024/05/21 22:43

【正式开始前的唠嗑】——春节假期还是如期而终,各位小伙伴是不是和我一样患上了“假期综合症”呢?工作状态简直太差了!博客也好久没写了,现在来总结总结struts2中文件的下载和上传!

 

Part1——文件下载

 

struts2提供了stream结果类型专门用于支持文件下载,指定stream结果类型时需指定一个inputName参数,该参数指定了一个被下载文件的入口的输入流。

下载页面:

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Struts 2的文件下载</title></head><body><h1>Struts 2的文件下载</h1><ul><li>下载图片:<a href="download.action">下载图形文件</a> </li></ul></body></html>

 

 

相应的Action:

public class FileDownloadActionextends ActionSupport{// 该成员变量可以在配置文件中动态指定该值private String inputPath;// inputPath的setter方法public void setInputPath(String value){inputPath = value;}/*定义一个返回InputStream的方法,用于被下载文件的入口,且需要配置stream类型结果时指定inputName参数,inputName参数的值就是方法去掉get前缀、首字母小写的字符串*/public InputStream getDownloadFile() throws Exception{// ServletContext提供getResourceAsStream()方法// 返回指定文件对应的输入流return ServletActionContext.getServletContext().getResourceAsStream(inputPath);}}

 

 

struts.xml中的配置:

<?xml version="1.0" encoding="GBK"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 配置Struts 2应用的字符集 --><constant name="struts.i18n.encoding" value="GBK"/><package name="lee" extends="struts-default"><action name="download" class="org.crazyit.app.action.FileDownloadAction"><!-- 指定被下载资源的位置 --><param name="inputPath">/WEB-INF/images/疯狂联盟.jpg</param><!-- 配置结果类型为stream的结果 --><result name="success" type="stream"><!-- 指定下载文件的文件类型 --><param name="contentType">image/jpg</param><!-- 指定由getDownloadFile()方法返回被下载文件的InputStream --><param name="inputName">downloadFile</param><param name="contentDisposition">filename="wjc_logo.jpg"</param><!-- 指定下载文件的缓冲大小 --><param name="bufferSize">4096</param></result></action></package></struts>

 

 

注:1)contentType还包括诸如application/zip(压缩类型)、text/html(文本类型)等

       2)contentDisposition默认是inline(内联)的,默认情况下,文本、图片等文件会在网页中直接打开,压缩文件才会弹出保存对话框;修改成attachment;fileName="${filename}"才会无论啥类型的文件都会弹出保存询问对话框。

      3)如果想要在用户下载之前进行权限检查,就必须要让struts2来控制下载——例如可以根据ActionContext的getSession()方法取出HttpSession,透过session判断当前用户是否有下载权限,如果没有跳转到相应的处理页面(如登录、注册等),相应地,必须在struts.xml配置。

 

Part2——文件上传

 

struts2的文件上传更是比较常见的使用情况。

上传页面实例:

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>简单的文件上传</title></head><body><s:form action="upload"enctype="multipart/form-data"><s:textfield name="title" label="文件标题"/><s:file name="upload" label="选择文件"/><s:submit value="上传"/></s:form></body></html>

 相应地Action:

public class UploadAction extends ActionSupport{// 封装文件标题请求参数的属性private String title;// 封装上传文件域的属性private File upload;// 封装上传文件类型的属性private String uploadContentType;// 封装上传文件名的属性private String uploadFileName;// 直接在struts.xml文件中配置的属性private String savePath;// 接受struts.xml文件配置值的方法public void setSavePath(String value){this.savePath = value;}// 获取上传文件的保存位置private String getSavePath() throws Exception{return ServletActionContext.getServletContext().getRealPath(savePath);}// 各属性的setter和getter方法。。。@Overridepublic String execute() throws Exception{// 以服务器的文件保存地址和原文件名建立上传文件输出流FileOutputStream fos = new FileOutputStream(getSavePath()+ "\\" + getUploadFileName());FileInputStream fis = new FileInputStream(getUpload());byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0){fos.write(buffer , 0 , len);}fos.close();return SUCCESS;}}

 struts.xml配置:

 

<struts><!-- 设置该应用使用的字符集 --><constant name="struts.i18n.encoding" value="GBK"/><package name="lee" extends="struts-default"><!-- 配置处理文件上传的Action --><action name="upload" class="org.crazyit.app.action.UploadAction"><!-- 动态设置Action的属性值 --><param name="savePath">/uploadFiles</param><!-- 配置Struts 2默认的视图页面 --><result>/WEB-INF/content/succ.jsp</result></action></package></struts>

 注:1)关于文件上传的容量控制可在struts.xml中(全局:<constant name="struts.multipart.maxSize" value="20000" />;局部写在拦截器中:<param name="maxmumSize">20000</param>)或struts.properties中指定;

 

         2)处理上传的Action必须定义三个private属性: xxx, xxxFileName, xxxContentType, 其中xxx与上传页面的<s:file name="upload" label="选择文件"/>name属性值必须对应。同时不要忘记着三个属性的getter/setter方法。否则不一致的话会获取不到
        3)可在配置文件中为上传的action配置拦截器(被该Action配置包围),配置允许上传的文件类型和文件大小等信息。

 

 

 

0 0
原创粉丝点击