Struts2文件上传

注意:需要加入包commons-io.jar。

1、新建一个JSP文件upload.jsp,代码如下:

<%@ page language="java" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>文件上传</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	
  </head>
  
  <body>
  <fieldset>
  	<legend>单文件上传</legend>
    	<form action="upload/upload" enctype="multipart/form-data" method="post">
	    	<input type="file" name="upload">
	    	<input type="submit" value="上传">
    	</form>
    </fieldset>
    <fieldset> 
    	<legend>多文件上传</legend>   
    	<form action="upload/uploadmultiupload" enctype="multipart/form-data" method="post">
		   	<input type="file" name="multiupload"><br/>
		   	<input type="file" name="multiupload">
		   	<input type="submit" value="上传"><br/>
		   	<input type="file" name="multiupload"><br/> 	
    	</form>
    </fieldset>
  </body>
</html>

2、在action包下面新建一个类ActionUpload(还需在WebRoot下面新建一个文件夹upload),代码如下:

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

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

public class ActionUpload {

	/**
     * upload和multiupload必需与文件表单里面的属性相同
	 * upload可以为File或String类型
	 * 但最好为File类型
	 * 因为如果为String类型,最后还是要把它变成File类型
	 */
	private String upload;
	
	private String uploadFileName;

	private String uploadContentType;

	private File[] multiupload;

	private String[] multiuploadFileName;

	public String execute() {
		String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
		File saveDir = new File(realPath, uploadFileName);
		if (!saveDir.getParentFile().exists()) {
			saveDir.getParentFile().mkdirs();
		}
		try {
			FileUtils.copyFile(new File(upload), saveDir);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String multiupload(){
		String realPath=ServletActionContext.getServletContext().getRealPath("/upload");
		for(int i=0;i<multiupload.length;i++){
			File saveDir=new File(realPath,multiuploadFileName[i]);
			if(!saveDir.getParentFile().exists()){
				saveDir.mkdirs();
			}
			try{
				FileUtils.copyFile(multiupload[i], saveDir);
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		return "success";
	}
	public String getUpload() {
		return upload;
	}

	public void setUpload(String 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 File[] getMultiupload() {
		return multiupload;
	}

	public void setMultiupload(File[] multiupload) {
		this.multiupload = multiupload;
	}

	public String[] getMultiuploadFileName() {
		return multiuploadFileName;
	}

	public void setMultiuploadFileName(String[] multiuploadFileName) {
		this.multiuploadFileName = multiuploadFileName;
	}

}

注意:可在struts.xml文件中配置上传文件上限

<constantname="struts.multipart.maxSize"value="1024000"/>

相关推荐