欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2文件上传和下载

程序员文章站 2022-07-12 16:14:30
...
1. struts2文件上传和下载

1) Struts2文件上传
Struts2文件上传基于Struts2拦截器实现;
Struts2文件上传使用的是fileupload组件;
Form配置enctype="multipart/form-data";
Struts2获取上传文件:name(name是文件表单的name)
Struts2获取上传文件名:name+FileName;
Struts2获取上传文件的类型:name+ContentType;

2) 配置文件的大小及类型
<paramname="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
<paramname="maximumSize">81101</param>
<s:fielderror></s:fielderror>


2. 大文件上传

Struts2文件上传大小默认是2M;
<constantname="struts.multipart.maxSize"value="20000000"></constant>


FileUploadAction.java

package com.andrew.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
    private File andrew; // 文件
    private String andrewFileName;  // 文件名
    private String andrewContentType;  // 文件类型
    public File getandrew() {
        return andrew;
    }
    public void setandrew(File andrew) {
        this.andrew = andrew;
    }
    public String getandrewFileName() {
        return andrewFileName;
    }
    public void setandrewFileName(String andrewFileName) {
        this.andrewFileName = andrewFileName;
    }
    public String getandrewContentType() {
        return andrewContentType;
    }
    public void setandrewContentType(String andrewContentType) {
        this.andrewContentType = andrewContentType;
    }
    @Override
    public String execute() throws Exception {
        System.out.println("文件名:" + andrewFileName);
        System.out.println("文件类型:" + andrewContentType);
        String filePath="E:/" + andrewFileName;
        File saveFile = new File(filePath);
        FileUtils.copyFile(andrew, saveFile);
        return SUCCESS;
    }
}

struts.xml

<constant name="struts.multipart.maxSize" value="20000000"></constant>
<package name="manager" extends="struts-default">
    <action name="upload" class="com.andrew.action.FileUploadAction">
        <result name="success">/success.jsp</result>
        <result name="input">/fileupload.jsp</result>
        <interceptor-ref name="fileUpload">
            <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
            <param name="maximumSize">81101</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>
    </action>
</package>

fileupload.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<s:fielderror></s:fielderror>
<form action="upload" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="andrew"/><br/>
    <input type="submit" value="提交"/>
</form>

success.jsp

文件上传成功!

http://localhost:8080/HeadFirstStruts2Chap08/fileupload.jsp
submit
http://localhost:8080/HeadFirstStruts2Chap08/upload
文件上传成功!


3. 多文件上传

FilesUploadAction.java

package com.andrew.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FilesUploadAction extends ActionSupport {
    private File[] files; // 文件
    private String[] filesFileName; // 文件名
    private String[] filesContentType; // 文件类型
    public File[] getFiles() {
        return files;
    }
    public void setFiles(File[] files) {
        this.files = files;
    }
    public String[] getFilesFileName() {
        return filesFileName;
    }
    public void setFilesFileName(String[] filesFileName) {
        this.filesFileName = filesFileName;
    }
    public String[] getFilesContentType() {
        return filesContentType;
    }
    public void setFilesContentType(String[] filesContentType) {
        this.filesContentType = filesContentType;
    }
    @Override
    public String execute() throws Exception {
        for (int i = 0; i < files.length; i++) {
            System.out.println("文件名:" + filesFileName[i]);
            System.out.println("文件类型:" + filesContentType[i]);
            String filePath = "E:/" + filesFileName[i];
            File saveFile = new File(filePath);
            FileUtils.copyFile(files[i], saveFile);
        }
        return SUCCESS;
    }
}

struts.xml

<constant name="struts.multipart.maxSize" value="20000000"></constant>
<package name="manager" extends="struts-default">
    <action name="uploads" class="com.andrew.action.FilesUploadAction">
        <result name="success">/success.jsp</result>
        <result name="input">/filesupload.jsp</result>
    </action>
</package>

filesupload.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<s:fielderror></s:fielderror>
<form action="uploads" method="post" enctype="multipart/form-data">
    文件1:<input type="file" name="files"/><br/>
    文件2:<input type="file" name="files"/><br/>
    文件3:<input type="file" name="files"/><br/>
    <input type="submit" value="提交"/>
</form>

success.jsp

文件上传成功!

http://localhost:8080/HeadFirstStruts2Chap08/filesupload.jsp
submit
http://localhost:8080/HeadFirstStruts2Chap08/uploads
文件上传成功!


4. struts2文件下载

返回的是文件流
<paramname="contentDisposition">attachment;filename=${fileName}</param>
InputStreamgetInputStream()

FileDownloadAction.java

package com.andrew.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
    private String fileName;
    public String getFileName() throws Exception {
        fileName = new String(fileName.getBytes(), "ISO8859-1");
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public InputStream getInputStream() throws Exception {
        File file = new File("E:/1.jpg");
        this.fileName = "美女1号.jpg";
        return new FileInputStream(file);
    }
}

struts.xml

<package name="manager" extends="struts-default">
    <action name="download" class="com.andrew.action.FileDownloadAction">
        <result type="stream">
            <param name="contentDisposition">attachment;filename=${fileName}</param>
        </result>
    </action>
</package>

http://localhost:8080/HeadFirstStruts2Chap08/filedownload.jsp
文件下载
相关标签: Java struts2