Struts2文件上传与下载

一、文件上传, Struts2文件上传基于拦截器,底层使用commons-fileupload

  文件上传对表单的要求,method=“post” , enctype="multipart/form-data",  表单中要有 file 类型的文本域

  1、创建上传文件的表单

  2、创建上传的Action类:接收的文件属性名要与表单中的一致     文件名称为  表单中的名字+fileName    、 文件类型为  表单中的名字+contentType  ,提供set  get方法

  

 1 public String upload() {
 2         ServletContext servletContext = ServletActionContext.getServletContext();
 3         String realPath = servletContext.getRealPath("/");
 4         String path = realPath + "/upload" + "/" + fileFileName;
 5         try {
 6             FileUtils.copyFile(file, new File(path));
 7         } catch (IOException e) {
 8             e.printStackTrace();
 9         }
10         return "success";
11     }

对于 多文件上传,接收文件属性为数组格式, 文件名称也为数组格式,提供set get方法

 1 public String uploads() {
 2         ServletContext servletContext = ServletActionContext.getServletContext();
 3         for(int i=0;i<file.length;i++) {
 4             String realPath = servletContext.getRealPath("/");
 5             realPath = realPath +"/upload"+ "/" + fileFileName[i];
 6             try {
 7                 FileUtils.copyFile(file[i], new File(realPath));
 8             } catch (IOException e) {
 9                 e.printStackTrace();
10             }
11         }
12         return "success";
13     }

限制上传文件格式,利用拦截器

<action name="up" class="com.tx.action.UploadAction" method="upload">
        <!-- 主动引入拦截器 ,设置上传文件类型位jpg  txt格式-->
            <interceptor-ref name="defaultStack">
                <param name="fileUpload.allowedExtensions">.jpg,.txt</param>
                <!-- <param name="fileUpload.maximumSize">1048576</param> -->
            </interceptor-ref>
            <result name="success">/success.jsp</result>
            <result name="input">/form.jsp</result>
        </action>

对于文件上传大小的中文显示,需要配置国际化文件,建立国际化文件并在struts2中设置

<!-- 配置文件大小限制 -->
    <constant name="struts.multipart.maxSize" value="1048576"></constant>
    <!-- 配置国际化 -->
    <constant name="struts.custom.i18n.resources" value="com/tx/resource/msg_zh_CN"></constant>

二、文件下载

  创建Action类,必须有三个属性:  提供输入流的属性inputStream(名称固定)  、文件名  fileName、 int类型文件大小 filelength ,提供set get方法

  

 1 public String download() throws Exception {
 2         ServletContext servletContext = ServletActionContext.getServletContext();
 3         String realPath = servletContext.getRealPath("/upload/1.jpg");
 4         //实例化输入流
 5         inputStream = new FileInputStream(new File(realPath));
 6         //给fileName赋值
 7         fileName = FilenameUtils.getName(realPath);
 8         //对文件名编码
 9         fileName = URLEncoder.encode(fileName, "UTF-8");
10         //给filelength赋值
11         filelength = inputStream.available();
12         return "success";
13     }

  再在xml文件中配置

 1 <action name="down" class="com.tx.action.DownloadAction" method="download">
 2             <result name="success" type="stream">
 3                 <!-- 指定Action中输入流变量 -->
 4                 <param name="inputStream">inputStream</param>
 5                 <!-- 设置响应的消息头 -->
 6                 <param name="contentDisposition">attachment;filename=${fileName}</param>
 7                 <!-- 使用下载的方式返回结果 -->
 8                 <param name="contentType">application/octet-stream</param>
 9                 <!-- 配置文件大小 -->
10                 <param name="contentLength">${filelength}</param>
11             </result>
12         </action>

 

posted @ 2018-03-25 19:21  cat_fish  阅读(146)  评论(0编辑  收藏  举报