Struts2的多文件上传 和 单文件下载

Jar包不变

Action类有改变

都改成 集合  例如:List<String>

 

1.1 Action

package cn.happy.struts10files;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by linlin on 2017/10/30.
 */
public class FileAction implements Action{

    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;
    private String savePath;
    public String execute() throws Exception {
        byte[] buffer=new byte[1024];
        int index=0;
        for(File item:upload) {
            FileInputStream fis = new FileInputStream(item);
            FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName().get(index++));
            int length = fis.read(buffer);
            while (length > 0) {
                fos.write(buffer, 0, length);
                length = fis.read(buffer);
            }
            fos.flush();
            fos.close();
            fis.close();
        }

        return SUCCESS;
    }

    public List<File> getUpload() {
        return upload;
    }

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

    public List<String> getUploadContentType() {
        return uploadContentType;
    }

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

    public List<String> getUploadFileName() {
        return uploadFileName;
    }

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

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

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

1.2 Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>


<constant name="struts.multipart.maxSize" value="55000000"></constant>
    <package name="day-10" namespace="" extends="struts-default">

        <action name="moreuploadAction" class="cn.happy.struts10files.FileAction">
          <param name="savePath">/upload</param>
            <result name="success">/file/strutsupload.jsp</result>
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">1000000</param>
            </interceptor-ref>
        </action>

    </package>

</struts>

1.3页面



<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/11/5
  Time: 16:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<s:form action="moreuploadAction" enctype="multipart/form-data" method="post">
    <s:file name="upload" label="选择文件"/><br/>
    <s:file name="upload" label="选择文件"/><br/>
    <s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>


<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/11/5
  Time: 16:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<s:iterator value="uploadFileName">
    文件:<s:property/> <br/>
</s:iterator>
<s:iterator value="uploadContentType">
    文件类型:<s:property/> <br/>
</s:iterator>
</body>
</html>



文件下载:

1.1Action

package cn.happy.struts11down;

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

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * Created by linlin on 2017/11/5.
 */
public class FileDownAction extends ActionSupport {

    private String inputPath;
    private String fileName;
    private InputStream inputStream;
    private String contentType;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String getInputPath() {
        return inputPath;
    }

    public void setInputPath(String inputPath) {
        this.inputPath = inputPath;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public InputStream getInputStream() throws FileNotFoundException {
        String path= ServletActionContext.getServletContext().getRealPath(inputPath);

        return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

1.2Struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>


    <package name="day-11" namespace="" extends="struts-default">
        <!--文件下载-->
        <action name="filedownAction" class="cn.happy.struts11down.FileDownAction">
            <!-- 设置文件路径的参数,传到action类文件中去 -->
            <param name="inputPath">/upload</param>
            <!-- 下载文件类型定义,即定义为“stream” -->
            <result name="success" type="stream">
                <param name="contentType">application/octet-stream</param>
                <!-- 下载文件输出流定义 -->
                <!-- 这里的inputName元素所对应的value值downloadFile,在action中一定要有对应的getDownloadFile()方法 -->
                <param name="inputName">inputStream</param>
                <!-- 下载文件处理方法 -->
                <param name="contentDisposition">attachment;filename="${fileName}"</param><!--设置响应的HTTP头信息中的Content-Disposition参数的值-->
                <!-- 下载缓冲区的大小 -->
                <param name="bufferSize">4096</param>
            </result>
        </action>

    </package>

</struts>

1.3页面


<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/11/5
  Time: 17:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="filedownAction.action?fileName=8.jpg">文件下载</a></body>
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值