Loading

Struts2文件的上传与下载

文件上传

1.配置JAR

2.创建上传Action

package com.fileUpload.action;

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

import java.io.*;

/**
 * 如果上传文件为文本空文件,会报错
 */
public class SimpleFileUploadAction extends ActionSupport {
    private static final int BUFFER_SIZE = 1024;
    private File upload; //封装上传文件域的属性
    private String uploadContentType; //封装上传文件的类型
    private String uploadFileName; //封装上传文件名
    private String savePath; //封装上传文件的保存路径

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    /**
     * 将源文件复制成目标文件
     * @param source 源文件对象
     * @param target 目标文件对象
     */
    private static void copy(File source,File target){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(source),BUFFER_SIZE);
            outputStream = new BufferedOutputStream(new FileOutputStream(target),BUFFER_SIZE);
            byte[] bytes = new byte[BUFFER_SIZE];
            int len = 0;
            while((len = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public String execute() throws Exception {
        //根据服务器的文件保存地址和源文件名创建目标文件全路径
        String realPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+this.getUploadFileName();
        File target = new File(realPath);
        copy(this.upload,target);
        return SUCCESS;

    }
}


3.配置struts.xml


4.配置过滤器

5.上传界面与显示界面

上传界面(source.jsp)

<%--
  Created by IntelliJ IDEA.
  User: HZC
  Date: 2021/4/13
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <s:form action="simpleFileUpload.action" method="post" enctype="multipart/form-data">
        <h1>单文件上传</h1>
        <s:file name="upload"  size="20"></s:file>
        <s:submit value="上传"></s:submit>
    </s:form>
<hr>
    <s:form action="moreFileUpload.action" method="post" enctype="multipart/form-data">
        <h1>多文件上传</h1>
        <s:file name="upload" size="20"></s:file>
        <s:file name="upload" size="20"></s:file>
        <s:file name="upload" size="20"></s:file>
        <s:submit value="上传"></s:submit>
    </s:form>
</body>
</html>

显示界面(target.jsp)

<%--
  Created by IntelliJ IDEA.
  User: HZC
  Date: 2021/4/13
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--    <s:iterator value="uploadFileName" status="s">--%>
<%--        <img src="upload/<s:property value="uploadFileName[#s.getIndex()]"/>" />--%>
<%--    </s:iterator>--%>
    <table>
        <s:iterator value="uploadFileName" status="s" var="upload">
            <tr>
                <td>
                    <a href = "download.action?downPath=upload/<s:property value="#upload" /> ">
                        <s:property value="#upload"></s:property>
                    </a>
                </td>
            </tr>
        </s:iterator>
    </table>

</body>
</html>


文件下载

1.配置下载Action

package com.fileUpload.action;

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

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class DownloadAction extends ActionSupport {
    private String downPath;//下载文件的位置
    //返回InputStream流方法
    public InputStream getInputStream(){
        return ServletActionContext.getServletContext().getResourceAsStream(downPath);
    }
    //文件名转换编码,防止中文乱码
    public String getDownloadFileName(){
        String downFileName = downPath.substring(7);
        downFileName = new String(downFileName.getBytes(StandardCharsets.UTF_8));
        return downFileName;
    }
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String getDownPath() {
        return downPath;
    }

    public void setDownPath(String downPath) {
        this.downPath = downPath;
    }
}

2.配置下载struts.xml

附:

使用List集合实现文件上传


posted @ 2021-04-14 17:34  IamHzc  阅读(110)  评论(0编辑  收藏  举报