10-struts2文件上传与下载

1.上传

1.web基础的文件上传

 

  • 浏览器端:

1.method=post
2.<input type="file" name="xx">
3.encType="multipart/form-data";

  • 服务器端:

commons-fileupload组件
1.DiskFileItemFactory
2.ServletFileUpload
3.FileItem


2.struts2中文件上传:

  • 导入文件下载上传的两个jar文件,一个是commons-fileupload-1.2.2.jar,另一个是commons-io-2.0.1.jar

默认情况下struts2框架使用的就是commons-fileupload组件.
struts2它使用了一个interceptor帮助我们完成文件上传操作。
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

提供 FileUpload 拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容

fileUpload拦截器 默认在 defaultStack 栈中, 默认会执行的


  • 页面上组件:<input type="file" name="upload">在进行文件上传时,表单提交方式一定要是post的方式,因为文件上传时二进制文件可能会很大,还有就是enctype属性,这个属性一定要写成multipart/form-data,不然就会以二进制文本上传到服务器端

 

  • 在action中怎样处理文件上传?

在action中要有三个属性:

private File upload;// 这里变量名 和 页面表单元素 name 属性一致。注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件


private String uploadContentType;//提交过来的file的MIME类型


private String uploadFileName;//提交过来的file的名字



  • 在execute方法中使用commons-io包下的FileUtils完成文件复制.

FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));

 

单文件上传代码:

          1.jsp页面

<form action="${pageContext.request.contextPath}/up" method="post" enctype="multipart/form-data">
        <input type="file" name="file"><br>
        <input type="submit" value="上传" >
    </form>

          2.action类

package cn.itcast.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
    private File file;// 这里变量名 和 页面表单元素 name
                        // 属性一致。注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件

    private String fileContentType;// 提交过来的file的MIME类型,固定名字(看file名)

    private String fileFileName;// 提交过来的file的名字,固定名字(看file名)

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    @Override
    public String execute() throws Exception {

        FileUtils.copyFile(file, new File("d:/upload", fileFileName));// 上传到本地磁盘d盘的upload文件夹下
        System.out.println("上传成功");
        return null;
    }
}

          

          3.struts.xml配置

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="up" class="cn.itcast.action.UploadAction"></action>
    </package>
</struts>

          4.问题:当上传组件的name为upload有时候会重定向,然后就会404

              没有解决,不知道什么问题。

 

3.关于struts2中文件上传细节:

 

  • 1.关于控制文件上传大小

在default.properties文件中定义了文件上传大小
struts.multipart.maxSize=2097152 上传文件默认的总大小 2m

  • 2.在struts2中默认使用的是commons-fileupload进行文件上传。

# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=jakarta

如果使用pell,cos进行文件上传,必须导入其jar包.

  • 3.如果出现问题,需要配置input视图,在页面上可以通过<s:actionerror>展示错误信息.

问题:在页面上展示的信息,全是英文,要想展示中文,国际化

struts-messages.properties 文件里预定义 上传错误信息,通过覆盖对应key 显示中文信息
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}

修改为
struts.messages.error.uploading=上传错误: {0}
struts.messages.error.file.too.large=上传文件太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=上传文件的类型不允许: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=上传文件的后缀名不允许: {0} "{1}" "{2}" {3}

{0}:<input type=“file” name=“uploadImage”>中name属性的值
{1}:上传文件的真实名称
{2}:上传文件保存到临时目录的名称
{3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)

      • 4.关于多文件上传时的每个上传文件大小控制以及上传文件类型控制.


1.多文件上传
服务器端:
只需要将action属性声明成List集合或数组就可以。

private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;

2.怎样控制每一个上传文件的大小以及上传文件的类型?
在fileupload拦截器中,通过其属性进行控制.

maximumSize---每一个上传文件大小
allowedTypes--允许上传文件的mimeType类型.
allowedExtensions--允许上传文件的后缀名.

<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
</interceptor-ref>


2.下载


1.web基础文件下载方式:

 

  • 1.超连接

 

  • 2.服务器编码,通过流向客户端写回。


1.通过response设置 response.setContentType(String mimetype);
2.通过response设置 response.setHeader("Content-disposition;filename=xxx");
3.通过response获取流,将要下载的信息写出。


2.struts2中文件下载:


通过<result type="stream">完成。

<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
在StreamResult类中有三个属性:
protected String contentType = "text/plain"; //用于设置下载文件的mimeType类型
protected String contentDisposition = "inline";//用于设置进行下载操作以及下载文件的名称
protected InputStream inputStream; //用于读取要下载的文件。

  • 在action类中定义一个方法

public InputStream getInputStream() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:/upload/" + filename);
return fis;
}

  • 在struts.xml中

  <result type="stream">

<param name="contentType">text/plain</param>
<param name="contentDisposition">attachment;filename=a.txt</param>
<param name="inputStream">${inputStream}</param> 会调用当前action中的getInputStream方法。
</result>


问题1:<a href="${pageContext.request.contextPath}/download?filename=捕获.png">捕获.png</a>下载报错


原因:超连接是get请求,并且下载的文件是中文名称,乱码。


问题2:下载捕获文件时,文件名称就是a.txt ,下载文件后缀名是png,而我们在配置文件中规定就是txt?


<result type="stream">
<param name="contentType">${contentType}</param> <!-- 调用当前action中的getContentType()方法 -->
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
<param name="inputStream">${inputStream}</param><!-- 调用当前action中的getInputStream()方法 -->
</result>

在struts2中进行下载时,如果使用<result type="stream">它有缺陷,例如:下载点击后,取消下载,服务器端会产生异常。
在开发中,解决方案:可以下载一个struts2下载操作的插件,它解决了stream问题。

原文地址:https://www.cnblogs.com/1963942081zzx/p/6491143.html