struts上传需要的jar包 (commons-fileupload-1.2.1.jar, commons-io-1.3.2.jar)
jsp客户端请求
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 简单的文件上传 </title>
<meta name="author" content="Yeeku.H.Lee" />
<meta name="website" content="http://www.crazyit.org" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="upload.action" method="post"
enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br />
选择文件:<input type="file" name="upload" /><br />
<input value="上传" type="submit" />
</form>
</body>
</html>
Action处理(参数要注意)uploadContentType封装文件类型, uploadFileName封装文件名, upload,title要对应jsp的name参数(部分set和get方法没写)
package com.lbx.action;
import java.io.File;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport implements
ServletContextAware {
private File upload;// 实际上传文件
private String uploadContentType; // 文件的内容类型
private String uploadFileName; // 上传文件名
private String title;// 上传文件时的备注
private ServletContext context;
public String execute() throws Exception {
try {
String targetDirectory = context.getRealPath("/upload");
String targetFileName = uploadFileName;
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(upload, target);
setUploadFileName(target.getPath());//保存文件的存放路径
} catch (Exception e) {
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ServletContext getContext() {
return context;
}
public void setContext(ServletContext context) {
this.context = context;
}
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 void setServletContext(ServletContext context) {
this.context = context;
}
}
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.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="login" namespace="/" extends="struts-default">
<action name="upload" class="com.lbx.action.UploadAction">
<param name="savePath">/upload</param>
<result>
/success.jsp
</result>
</action>
</package>
</struts>