struts2之文件上传
阅读原文时间:2023年07月15日阅读:1

一、单文件上传

实例:

表单应该注意三个点   form中的method="post"、enctype="multipart/form-data"、input中的type="file"

fileForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

File Operation


struts.xml中配置文件

/WEB-INF/pages/file.jsp /WEB-INF/pages/error.jsp

FileOperationAction.java

package com.lz.web.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileOperation extends ActionSupport{

private File myFile;  
private String myFileContentType;  
private String myFileFileName;  
private String destPath;

public File getMyFile() {  
    return myFile;  
}

public void setMyFile(File myFile) {  
    this.myFile = myFile;  
}

public String getMyFileContentType() {  
    return myFileContentType;  
}

public void setMyFileContentType(String myFileContentType) {  
    this.myFileContentType = myFileContentType;  
}

public String getMyFileFileName() {  
    return myFileFileName;  
}

public void setMyFileFileName(String myFileFileName) {  
    this.myFileFileName = myFileFileName;  
}

public String upload(){

    destPath= ServletActionContext.getServletContext().getRealPath("/files");//文件保存的路径  
    System.out.println("realpath: "+destPath);  
    System.out.println("Src filename: "+myFile);  
    System.out.println("Dest filename: "+myFileFileName);  
    try {  
        File destFile = new File(destPath, myFileFileName);  
        FileUtils.copyFile(myFile, destFile);//拷贝文件  
    } catch (IOException e) {  
        e.printStackTrace();  
        return "error";  
    }

    return "upload";  
}  
public String prefile(){

    return "prefile";  
}  

}

file.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

Insert title here your have successfully uploaded

二、多文件上传

fileForm2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

File Operation

文件1:
文件2:
文件3:

struts.xml中配置

/WEB-INF/pages/fileForm2.jsp /WEB-INF/pages/file2.jsp

MultiFileUploadAction.java

package com.lz.web.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class MultiFileUploadAction extends ActionSupport{

private File\[\] image;  
private String\[\] imageFileName;  
private String\[\] imageContentType;

public File\[\] getImage() {  
    return image;  
}  
public void setImage(File\[\] image) {  
    this.image = image;  
}  
public String\[\] getImageFileName() {  
    return imageFileName;  
}  
public void setImageFileName(String\[\] imageFileName) {  
    this.imageFileName = imageFileName;  
}  
public String\[\] getImageContentType() {  
    return imageContentType;  
}  
public void setImageContentType(String\[\] imageContentType) {  
    this.imageContentType = imageContentType;  
}  
public String load(){  
    return "load";  
}  
public String upload(){

    String realPath = ServletActionContext.getServletContext().getRealPath("/files");//指定文件保存的路径  
    System.out.println("realpath:  "+realPath);  
    try {  
        if(image!=null){  
            File savedir = new File(realPath);  
            if(!savedir.getParentFile().exists())//判断文件目录是否存在  
                savedir.getParentFile().mkdirs();  
            for(int i=0; i<image.length; i++){  
                File saveFile = new File(realPath, imageFileName\[i\]);  
                FileUtils.copyFile(image\[i\], saveFile);//文件复制  
            }  
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return "upload2";  
}

}

file2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

Insert title here 上传成功