zoukankan      html  css  js  c++  java
  • Strusts2笔记8--文件的上传和下载

    文件的和上传和下载:

      (1)文件的上传:

        Struts是通过拦截器实现文件上传的,而默认拦截器栈中包含了文件上传拦截器,故表单通过Struts2可直接将文件上传,其底层是通过apache的commons-fileupload完成的。

        我们要做的,就是将上传好的文件放到指定的位置或者其他的一些处理

        前端表单提交的代码:

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 
     3 <html>
     4   <head>
     5     <title>index page</title>
     6   </head>
     7   
     8   <body>
     9     <form action="test/upload.action" method="POST" enctype="multipart/form-data">
    10         文件:<input type="file" name="img"/><br>
    11         <input type="submit" value="上传"/>
    12     </form>
    13   </body>
    14 </html>

        Action代码:

     1 package com.tongji.actions;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 
     6 import org.apache.commons.io.FileUtils;
     7 import org.apache.struts2.ServletActionContext;
     8 
     9 public class UploadAction {
    10     private File img;
    11     private String imgFileName;  //文件名 = 文件 + FileName , 必须是这样
    12     
    13     public File getImg() {
    14         return img;
    15     }
    16 
    17     public void setImg(File img) {
    18         this.img = img;
    19     }
    20 
    21     public String getImgFileName() {
    22         return imgFileName;
    23     }
    24 
    25     public void setImgFileName(String imgFileName) {
    26         this.imgFileName = imgFileName;
    27     }
    28 
    29     public String execute() throws IOException {
    30         if (img != null) {
    31             //String path = "d:/images"; 
    32             String path = ServletActionContext.getServletContext().getRealPath("/images");
    33             File destFile = new File(path,imgFileName);
    34             FileUtils.copyFile(img, destFile);
    35             return "success";
    36         }
    37         return "message";
    38     }
    39     
    40 }

        注意:第32行,不能像31行那样直接写文件要被拷贝到的路径。因为,不能让前端代码操纵后端服务器的盘符,前端代码能够操作的只能是对应项目的所在空间(tomcat目录下的项目的空间)。

        struts.xml配置文件: 

     1 <?xml version="1.0" encoding="UTF-8"?>
     2     <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <constant name="struts.multipart.maxSize" value="20971520"/>     <!-- 改变可上传文件的大小 -->
     7     <package name="demo" namespace="/test" extends="struts-default">
     8         <action name="upload" class="com.tongji.actions.UploadAction">
     9             <result>/welcome.jsp</result>
    10             <result name="message">/messge.jsp</result>    <!-- 处理上传失败异常 -->
    11             <interceptor-ref name="defaultStack">    <!-- 设置可上传文件的拓展名 -->
    12                 <param name="fileUpload.allowedExtensions">jpg,png</param>
    13             </interceptor-ref>
    14         </action>
    15     </package>
    16 </struts>

        注意:以上的注释内容。

      (2)多文件的上传:

        前端表单提交的代码:

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 
     3 <html>
     4   <head>
     5     <title>index page</title>
     6   </head>
     7   
     8   <body>
     9     <form action="test/upload.action" method="POST" enctype="multipart/form-data">
    10         文件1:<input type="file" name="imgs"/><br>
    11         文件2:<input type="file" name="imgs"/><br>
    12         文件3:<input type="file" name="imgs"/><br>
    13         <input type="submit" value="上传"/>
    14     </form>
    15   </body>
    16 </html>

        Action代码:

     1 package com.tongji.actions;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 
     6 import org.apache.commons.io.FileUtils;
     7 import org.apache.struts2.ServletActionContext;
     8 
     9 public class UploadAction {
    10     private File[] imgs;
    11     private String[] imgsFileName;  //文件名 = 文件 + FileName , 必须是这样
    12     
    13     public File[] getImgs() {
    14         return imgs;
    15     }
    16 
    17     public void setImgs(File[] imgs) {
    18         this.imgs = imgs;
    19     }
    20 
    21     public String[] getImgsFileName() {
    22         return imgsFileName;
    23     }
    24 
    25     public void setImgsFileName(String[] imgsFileName) {
    26         this.imgsFileName = imgsFileName;
    27     }
    28 
    29     public String execute() throws IOException {
    30         //若没有选择要上传的文件,则该数组不进行创建,而非创建了长度为0的数组对象
    31         if (imgs != null) {
    32             String path = ServletActionContext.getServletContext().getRealPath("/images");
    33             for (int i = 0; i < imgs.length; i++) {
    34                 File destFile = new File(path, imgsFileName[i]); 
    35                 FileUtils.copyFile(imgs[i], destFile);
    36             }
    37             return "success";
    38         }
    39         return "message";
    40     }
    41     
    42 }

         补充:<constant name="struts.multipart.maxSize" value="20971520"/> <!-- 改变可上传文件的大小 -->,在多文件上传时,指的是多文件上传的总大小

      (3)文件的下载:

        服务端向客户端浏览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示;如果需要用户以附件的形式保存,则称为文件下载

        如果需要向浏览器提供文件下载功能,则需要设置HTTP响应头的Content-Disposition属性,即内容配置属性值为attachment(附件)。

        Action类中需要提供两个属性,一个为文件输入流,用于指定服务器向客户端所提供下载的文件资源;一个为文件名,即用户要下载的资源文件名

        前端页面代码: 

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 
     3 <html>
     4   <head>
     5     <title>index page</title>
     6   </head>
     7   
     8   <body>
     9     <a href="test/download.action">美图</a>
    10   </body>
    11 </html>

        Action代码:

     1 package com.tongji.actions;
     2 
     3 import java.io.InputStream;
     4 
     5 import org.apache.struts2.ServletActionContext;
     6 
     7 public class DownloadAction {
     8     private InputStream is;
     9     private String fileName;  //随意
    10     
    11     public InputStream getIs() {
    12         return is;
    13     }
    14 
    15     public void setIs(InputStream is) {
    16         this.is = is;
    17     }
    18 
    19     public String getFileName() {
    20         return fileName;
    21     }
    22 
    23     public void setFileName(String fileName) {
    24         this.fileName = fileName;
    25     }
    26 
    27     public String execute() throws Exception {
    28         fileName = "5.jpg";
    29         
    30         fileName = "书言.jpg";
    31         is = ServletActionContext.getServletContext().getResourceAsStream("/images/" + fileName);
    32         
    33         byte[] bytes = fileName.getBytes("utf-8");
    34         fileName = new String(bytes, "iso-8859-1");
    35         
    36         return "success";
    37     }
    38     
    39 }

        解释:第33行是为了解决中文乱码的问题,因为代码是utf-8格式的,而前端页面是iso-8859-1格式的。

        struts.xml配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2     <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <package name="demo" namespace="/test" extends="struts-default">
     7         <action name="download" class="com.tongji.actions.DownloadAction">
     8             <result type="stream">
     9                 <param name="inputName">is</param>
    10                 <param name="contentDisposition">attachment;filename=${fileName}</param>
    11             </result>
    12         </action>
    13     </package>
    14 </struts>

        解释:返回类型是stream,并且要指定inputName(用于指定服务器向客户端所提供下载的文件资源),以及需要设置HTTP响应头的Content-Disposition属性,即配置attachment和文件名。

           如果Action中的InputStream属性值为inputStream,则不需要配置<param name="inputName">is</param>,因为InputStream默认值就为inputStream。

  • 相关阅读:
    CODING x 百果园 _ 水果零售龙头迈出 DevOps 体系建设第一步
    Nocalhost 亮相 CD Foundation 国内首届 Meetup,Keith Chan 将出席致辞
    做云原生时代标准化工具,实现高效云上研发工作流
    打造数字化软件工厂 —— 一站式 DevOps 平台全景解读
    WePack —— 助力企业渐进式 DevOps 转型
    CODING Compass —— 打造行云流水般的软件工厂
    Nocalhost —— 让云原生开发回归原始而又简单
    CODING 代码资产安全系列之 —— 构建全链路安全能力,守护代码资产安全
    Nocalhost:云原生开发新体验
    使用 Nocalhost 开发 Kubernetes 中的 APISIX Ingress Controller
  • 原文地址:https://www.cnblogs.com/qjjazry/p/6242225.html
Copyright © 2011-2022 走看看