1、加载页面index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%--
--%> <%-- --%> <%-- | --%>
--%> <%-- --%> <%-- | --%>
2、处理页面uoload.jsp
<%@page
import="com.jspsmart.upload.SmartUpload,org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest"%>
<%@ page language="java"
import="java.util.*,java.io.*,java.lang.reflect.*,org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.*"
pageEncoding="UTF-8"%>
<%
Class> multi = request.getClass();
Field personNameField = multi.getDeclaredField("multi");
personNameField.setAccessible(true);
JakartaMultiPartRequest partRequest = (JakartaMultiPartRequest) personNameField
.get(request);
Class> fileClass = partRequest.getClass();
Field uploadFiles = fileClass.getDeclaredField("files");
uploadFiles.setAccessible(true);
Map fileMap = (Map) uploadFiles.get(partRequest);
System.out.println(fileMap);
if (null != fileMap && !fileMap.isEmpty()) {
List
for (DiskFileItem diskFile : fileItem) {
DiskFileItem disk = diskFile;
File temp = new File("d:\\tewt.doc");
disk.write(temp);
}
}
%>
3、本地处理action
/**
*
*/
package com.test.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
private File upload;
private String uploadContentType;
private String uploadFileName;
public File getUpload() {
return upload;
}
public void setUpload(File 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 HttpServletResponse getResponse() {
HttpServletResponse response = null;
try {
response = ServletActionContext.getResponse();
} catch (RuntimeException e) {
}
return response;
}
public HttpServletRequest getRequest() {
HttpServletRequest request = null;
try {
request = ServletActionContext.getRequest();
} catch (RuntimeException e) {
}
return request;
}
@Override
public String execute() throws Exception {
Map
boolean isMultipart = ServletFileUpload
.isMultipartContent(getRequest());
if (isMultipart) {// type=multipart/form-data
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(1024 * 1024);// 设置上传文件的最大容量
List
for (FileItem item : items) {
if (item.isFormField()) {// 如果不是文件类型
// String name = item.getFieldName(); 表单中某个控件的名称
// String value = item.getString(); 表单中某个控件的值
} else {
String filename = item.getName();
File f = new File(filename); // 保存的文件
item.write(f);// 保存文件
}
}
} else {
}
HttpServletRequest request = getRequest();
FileOutputStream fos = new FileOutputStream("d:\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
return SUCCESS;
}
}
4、web.xml配置
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">