如何来实现struts2 的文件上传:
如何来更改要上传的文件路径:
在web-xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 定义Struts 2的核心Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 让Struts 2的核心Filter拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Action处理类
package lee;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
// 接受依赖注入的属性
private String savePath;
// 接受依赖注入的方法
public void setSavePath(String value) {
this.savePath = value;
}
private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File getUpload() {
return (this.upload);
}
public String getUploadContentType() {
return (this.uploadContentType);
}
public String getUploadFileName() {
return (this.uploadFileName);
}
/*//执行上传功能
58 private void uploadFile(int i) throws FileNotFoundException, IOException {
59 try {
60 InputStream in = new FileInputStream(file.get(i));
61 String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
62 File uploadFile = new File(dir, this.getFileFileName().get(i));
63 OutputStream out = new FileOutputStream(uploadFile);
64 byte[] buffer = new byte[1024 * 1024];
65 int length;
66 while ((length = in.read(buffer)) > 0) {
67 out.write(buffer, 0, length);
68 }
69
70 in.close();
71 out.close();
*/
@Override
public String execute() throws Exception {
System.out.println("开始上传单个文件---");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
// 判断是否允许上传
String filterResult = filterType(this.getAllowTypes().split(","));
if (filterResult != null) {
ActionContext.getContext().put("typeError","您要上传的文件类型不正确");
return filterResult;
}
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ 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);
}
return SUCCESS;
}
public String filterType(String[] types) {
String fileType = this.getUploadContentType();
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
}
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
}
Struts.properties 文件
struts.locale=zh_CN
struts.i18n.encoding=GBK
struts.multipart.parser=Jakarta
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>
<include file="struts-default.xml" />
<constant name="struts.multipart.maxSize" value="20971520" />
<constant name="struts.custom.i18n.resources"
value="globalMessages" />
<constant name="struts.i18n.encoding" value="GBK" />
<package name="lee" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<param name="allowTypes">
image/pjpeg,image/bmp,image/jpg,image/png,image/gif,image/jpeg
</param>
<param name="savePath">/upload</param>
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
在index.jsp中可以这样的写
<%@page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
${requestScope.typeError}
<form action="upload.action" method="post"
enctype="multipart/form-data">
<input type="text" name="title" />
<br>
<input type="file" name="upload" />
<br>
<input value="upload" type="submit" />
</form>
第二中实现方法:
<!-- 上传文件表单定义 -->
<s:form action="upload" method="post" enctype="multipart/form-data">
<tr>
<!-- 上传文件标签定义 -->
<td> 上传文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<td> 再次上传文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<td align="left"><s:submit name="submit" value="提交"></s:submit></td>
</tr>
</s:form>
</body>
</html>
上传成功的页面:
<%@ page language="java" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
上传成功!success.jsp
<br>
文件标题:
<s:property value=" + title" />
<br>
文件为:
<img src="<s:property value="'upload/' + uploadFileName"/>" />
<br>
</body>
</html>