Struts2通过commons-fileupload实现文件上传 - Java中文网整理校对
upload.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
< %@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>修改默认的提示信息</title>
</head>
<body>
<div style="color:red">
<!--采用红色的前景色,输出错误信息 -->
<s:fielderror/>
</div>
<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>
< %@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>修改默认的提示信息</title>
</head>
<body>
<div style="color:red">
<!--采用红色的前景色,输出错误信息 -->
<s:fielderror/>
</div>
<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>
succ.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
< %@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>
< %@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=" + title"/><br>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>
web.xml
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="upload" extends="struts-default">
<action name="upload" class="jCuckoo.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<param name="maximumSize">20000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<!-- 保存路径savePath依赖注入 -->
<param name="savePath">/upload</param>
<result name="input"> /upload.jsp</result>
<result>/succ.jsp</result>
</action>
</package>
</struts>
globalMessages.properties
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="upload" extends="struts-default">
<action name="upload" class="jCuckoo.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<param name="maximumSize">20000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<!-- 保存路径savePath依赖注入 -->
<param name="savePath">/upload</param>
<result name="input"> /upload.jsp</result>
<result>/succ.jsp</result>
</action>
</package>
</struts>
globalMessages.properties
struts.messages.error.content.type.not.allowed=您上传的文件类型只能是图片文件!请重新选择!
struts.messages.error.file.too.large=您要上传的文件太大,请重新选择!
UploadAction.java
struts.messages.error.file.too.large=您要上传的文件太大,请重新选择!
UploadAction.java
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
// 接受依赖注入的属性
private String savePath;
private String savePath;
// 接受依赖注入的方法
public void setSavePath(String value) {
this.savePath = value;
}
private String getSavePath() throws Exception {
return ServletActionContext.getRequest().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);
}
@Override
public String execute() throws Exception {
// 以服务器的文件保存地址和原文件名建立上传文件输出流
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 void setSavePath(String value) {
this.savePath = value;
}
private String getSavePath() throws Exception {
return ServletActionContext.getRequest().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);
}
@Override
public String execute() throws Exception {
// 以服务器的文件保存地址和原文件名建立上传文件输出流
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;
}
}