struts2:文件上传

文件上传

页面:

注意的是不要忘记了enctype="multipart/form-data">

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'fileUpload.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    	<form action="${pageContext.request.contextPath }/file/upload.action" method="post" enctype="multipart/form-data"> <!—在做文件上传的时候,不要忘记这个 -->
    		请选择文件:<input type="file" name="file" /><br/>
    		<input type="submit" value="上传" />
    	</form>
  </body>
</html>

struts.xml配置文件中

input的返回result

<!—文件上传的临时目录-->
<constant name="struts.multipart.saveDir" value="d://"/>	
	<package name="file" namespace="/file" extends="struts-default">
		<action name="upload" class="hwt.action.FileUploadAction">
		<!--  文件上传拦截器 -->
			<interceptor-ref name="fileUpload">
				<!—文件上传大小控制—>
				<param name="maximumSize">1024</param>
				<!—上传类型控制--->
				<param name="allowedTypes">
					 text/plain
					<!-- image/png,image/gif,image/jpeg -->
				</param>
			</interceptor-ref>
			<!-- 要放在后面,要不然不会起到作用 -->
			<interceptor-ref name="defaultStack"/>
			<!-- 还需要配置input为错误输出页面 -->
			<result name="input">/fileUpload.jsp</result>
			<result name="success">/downLoad.jsp</result>
			<result name="failure">/fileUpload.jsp</result>
		</action>
	</package>

Action中

注意: 继承ActionSupport 

/*
 * ActionSupport里面提供了validate的校验功能
 * 实际上继承ActionSupport之后 
 * 就等同于实现了很多接口 Action,Validateable,ValidationAWare,TextProvider,LoacalProvider,Serializable 
 * 可以提供数据校验 序列化 国际化等功能 
 */
public class FileUploadAction extends ActionSupport{
//记得生成setter/getter方法
	private File file ;
	//这个名字是struts2会自动填充,但是取名必须是-属性名ContentType;
	private String fileContentType; 
	//这个名字是struts2会自动填充,但是取名必须是-属性名FileName;
	private String fileFileName;
	
	public String execute() throws IOException{
		if (file != null) {
			String realPath = ServletActionContext.getServletContext().getRealPath(FileUploadUtils.baseName);
			
			String savepath = FileUploadUtils.getSavePath(realPath);
			String uuidFileName = FileUploadUtils.getUUIDName(fileFileName);
			
			File descFile = new File(savepath,uuidFileName);
			//这个工具类是commons.io包下面的
			FileUtils.copyFile(file, descFile);
			return "success";
		}else {
			return "failure";
		}
		
	}

	//setter getter
	...
}

工具类:

public class FileUploadUtils {
	public static String baseName;
	
	//单利模式得到属性文件中的文件上传的配置
	static{
		Properties fileProperties = new Properties();
		InputStream iStream =FileUploadUtils.class.getResourceAsStream("/fileUpload.properties");
		try {
			fileProperties.load(iStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		baseName = fileProperties.getProperty("basepath");
	}
	
	//把上传的文件名字改成UUID,解决相同文件名
	public static String getUUIDName(String fileName){
		UUID uuid = UUID.randomUUID();
		String extension = fileName.substring(fileName.lastIndexOf("."));
		return uuid+extension;
	}
	
	//得到服务器上的保存路径,便于查找
	public static String getSavePath(String realPath){
		//对上传的文件进行分级
		Calendar calendar = new GregorianCalendar();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day   = calendar.get(Calendar.DAY_OF_MONTH);
		String savePath = realPath+"\\"+year+"\\"+month+"\\"+day;
		File file = new File(savePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		System.out.println(savePath);
		return savePath;
	}
	
}




你可能感兴趣的:(struts2:文件上传)