struts2文件上传
struts2基于拦截器实现,使用fileupload组件(apache)。
- 编写fileupload.jsp和success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="newbeedaly"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
文件上传成功!
</body>
</html>
2.配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 大文件上传 -->
<constant name="struts.multipart.maxSize" value="20000000"></constant>
<package name="manager" extends="struts-default">
<action name="upload" class="com.newbeedaly.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>
</struts>
3.编写FileUploadAction类
package com.newbeedaly.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private File newbeedaly; // 文件
private String newbeedalyFileName; // 文件名
private String newbeedalyContentType; // 文件类型
public File getnewbeedaly() {
return newbeedaly;
}
public void setnewbeedaly(File newbeedaly) {
this.newbeedaly = newbeedaly;
}
public String getnewbeedalyFileName() {
return newbeedalyFileName;
}
public void setnewbeedalyFileName(String newbeedalyFileName) {
this.newbeedalyFileName = newbeedalyFileName;
}
public String getnewbeedalyContentType() {
return newbeedalyContentType;
}
public void setnewbeedalyContentType(String newbeedalyContentType) {
this.newbeedalyContentType = newbeedalyContentType;
}
@Override
public String execute() throws Exception {
System.out.println("文件名:"+newbeedalyFileName);
// 保存时注意要做文件类型的判断,不是需要的类型不能保存,后缀名自己手动写。
// 原因:hack可通过抓包工具修改后缀名。
System.out.println("文件类型:"+newbeedalyContentType);
String filePath="E:/"+newbeedalyFileName;
File saveFile=new File(filePath);
FileUtils.copyFile(newbeedaly, saveFile);
return SUCCESS;
}
}