Java 杂谈

Struts2学习笔记 | 关于文件的上传与下载

2019-08-07  本文已影响1人  一颗白菜_

文件上传前表单做的准备

这个在之前学习JSP的时候有接触到,这里再次说明一次。

文件上传

Demo如下:
UploadAction文件:

package com.cerr.struts2.upload;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class UploadAction extends ActionSupport {
    private File ppt;
    private String pptContentType;
    private String pptFileName;
    private String pptDesc;

    public String getPptDesc() {
        return pptDesc;
    }

    public void setPptDesc(String pptDesc) {
        this.pptDesc = pptDesc;
    }

    public File getPpt() {
        return ppt;
    }

    public void setPpt(File ppt) {
        this.ppt = ppt;
    }

    public String getPptContentType() {
        return pptContentType;
    }

    public void setPptContentType(String pptContentType) {
        this.pptContentType = pptContentType;
    }

    public String getPptFileName() {
        return pptFileName;
    }

    public void setPptFileName(String pptFileName) {
        this.pptFileName = pptFileName;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(ppt);
        System.out.println(pptContentType);
        System.out.println(pptFileName);
        System.out.println(pptDesc);
        ServletContext servletContext = ServletActionContext.getServletContext();
        String dir = servletContext.getRealPath("/files/"+pptFileName);

        //接下来就io操作
        FileOutputStream outputStream = new FileOutputStream(dir);
        FileInputStream inputStream = new FileInputStream(ppt);
        byte [] buffer = new byte[1024];
        int len = 0;
        while((len = inputStream.read(buffer))!= -1){
            outputStream.write(buffer,0,len);
        }
        outputStream.close();
        inputStream.close();
        return super.execute();
    }
}

一次上传多个文件

若传递多个文件,则上述提到的三个属性改为List类型并更新其gettersetter方法即可,示例:

private List<File> ppt;
private List<String> pptContentType;
private List<String> pptFileName;
private List<String> pptDesc;

返回的是对应的集合
如果上面的Demo要改成上传多个文件,则只需要把对一个对象的操作改成对一个集合对象的操作即可。

对上传进行限制

对上述的例子进行修改,添加限制
首先在struts.xml文件中定义中配置修改拦截器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <!-- 配置国际化资源文件 -->
    <constant name="struts.custom.i18n.resources" value="i18n"></constant>

    <package name="default" namespace="/" extends="struts-default">
        <!--自定义拦截器 -->
        <interceptors>
            <interceptor-stack name="cerrStack">
                <interceptor-ref name="defaultStack">
                    <param name="fileUpload.maximumSize">2000</param>
                    <param name="fileUpload.allowedTypes">text/html,text/xml</param>
                    <param name="fileUpload.allowedExtensions">html,dtd,xml</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <!-- 设置我们修改的该拦截器为默认的拦截器 -->
        <default-interceptor-ref name="cerrStack"></default-interceptor-ref>


        <action name="testUpload" class="com.cerr.struts2.upload.UploadAction">
            <result>/success.jsp</result>
            <result name="input">/upload.jsp</result>
        </action>
    </package>
</struts>

然后在国际化资源文件i18n.properties中定制错误消息

struts.messages.error.uploading=\u6587\u4ef6\u4e0a\u4f20\u51fa\u9519\u7684\u6d88\u606f
struts.messages.error.file.too.large=\u6587\u4ef6\u8d85\u8fc7\u6700\u5927\u503c\u7684\u6d88\u606f
struts.messages.error.content.type.not.allowed=\u6587\u4ef6\u5185\u5bb9\u7c7b\u578b\u4e0d\u5408\u6cd5\u7684\u6d88\u606f
struts.messages.error.file.extension.not.allowed=\u6587\u4ef6\u6269\u5c55\u540d\u4e0d\u5408\u6cd5\u7684\u6d88\u606f

这样就在上面代码的基础上添加了上传文件限制的功能了。
但是这样的话,实际上定制的消息并不完善,若要修改定制信息,则可以参考org.apache.struts2下的struts-messages.properties文件。


文件下载

<action name="testDownload" class="com.cerr.struts2.upload.DownLoadAction">
            <result type="stream">
                <param name="bufferSize">2048</param>
            </result>
</action>

例子:

package com.cerr.struts2.upload;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import java.io.FileInputStream;
import java.io.InputStream;

public class DownLoadAction extends ActionSupport {
    private String contentType;
    private long contentLength;
    private String contentDisposition;
    private InputStream inputStream;

    public String getContentType() {
        return contentType;
    }

    public long getContentLength() {
        return contentLength;
    }

    public String getContentDisposition() {
        return contentDisposition;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    @Override
    public String execute() throws Exception {
        //确定各个成员变量的值
        contentType="text/html";
        contentDisposition="attachment;filename=aa.png";
        ServletContext servletContext = ServletActionContext.getServletContext();
        String fileName = null;
        fileName = servletContext.getRealPath("/files/1.png");
        System.out.println(fileName);
        inputStream = new FileInputStream(fileName);
        contentLength = inputStream.available();
        return super.execute();
    }
}

上一篇 下一篇

猜你喜欢

热点阅读