struts2文件上传和下载

阅读更多
1. struts2文件上传和下载

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

2) 配置文件的大小及类型
image/bmp,image/x-png,image/gif,image/jpg,image/jpeg
81101



2. 大文件上传

Struts2文件上传大小默认是2M;



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



    
        /success.jsp
        /fileupload.jsp
        
            image/bmp,image/x-png,image/gif,image/jpg,image/jpeg
            81101
        
        
    


fileupload.jsp

<%@taglib prefix="s" uri="/struts-tags" %>

文件:
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



    
        /success.jsp
        /filesupload.jsp
    


filesupload.jsp

<%@taglib prefix="s" uri="/struts-tags" %>

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


4. struts2文件下载

返回的是文件流
attachment;filename=${fileName}
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


    
        
            attachment;filename=${fileName}
        
    


http://localhost:8080/HeadFirstStruts2Chap08/filedownload.jsp
文件下载

你可能感兴趣的:(Java,struts2)