1.配置web.xml(上一篇有)
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.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="false" />
<include file="com/struts/config/file.xml"></include>
</struts>
3. FileAction 类
package com.file.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileAction extends ActionSupport{
private File uploadFile;
private String uploadFileContentType;
private String uploadFileFileName;
public String file(){
System.out.println("11111111111111111111");
String realPath = "e:\\现计/piture";
File file = new File(realPath);
if(!file.exists()){
file.mkdirs();
}
try {
FileUtils.copyFile(uploadFile, new File(file, uploadFileFileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "input";
}
return "success";
}
/**********get 和 set方法************/
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.配置 file.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="16777216"></constant>
<package name="testo" extends="struts-default">
<action name="f_*" class="com.file.action.FileAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="input">/default.jsp</result>
</action>
</package>
</struts>
5.jsp页面访问
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>文件上传</title>
</head>
<body>
文件上传
<form action="f_file.action" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile">
<input type="submit" value="上传">
</form>
</body>
</html>