1.新建web工程,配置tomcat和struts2
2.下载Common-FileUpload框架
http://www.apache.org/,下载FileUpload和Commons IO两个jar包,加压后将两个jar包放到WEB-INF的lib目录下
3.action:FileUploadAction.java
package action;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
//设置上传文件的属性
private File uploadFile;
private String uploadFileContentType;
private String uploadFileFileName;
//get and set
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
InputStream is=new FileInputStream(uploadFile);
//设置文件上传目录
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
File toFile = new File(uploadPath,this.getUploadFileFileName());
OutputStream os = new FileOutputStream(toFile);
byte[] buffer = new byte[1024];
int length = 0;
while((length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
}
4.上传表单和完成后跳转界面
fileUploadPage.jsp:
<%@ page language="java" import ="java.util.*" 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>
<title>文件上传</title>
</head>
<body>
<form action="fileUpload.action" method="post" enctype="multipart/form-data">
上传文件:<input type="file" name="uploadFile" /><br/>
<input type="submit" value="上传" />
<input type="reset" value="重置" />
</form>
</body>
</html>
fileUploadResultPage.jsp:
<%@ page language="java" import="java.util.*" 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>
<title>文件上传成功</title>
</head>
<body>
上传文件名:${uploadFileFileName}<br/>
上传文件类型:${uploadFileContentType}
</body>
</html>
5.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6.配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="struts2" extends="struts-default">
<action name="fileUpload" class="action.FileUploadAction">
<result name="success">/fileUploadResultPage.jsp</result>
<result name="input">/fileUploadPage.jsp</result>
</action>
</package>
</struts>
7.注意事项
文件完成上传时不是在所建的工程中,而是在.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps中,而且上传目录文件夹必须
已经存在,否则会报错