1.创建upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts 2文件上传</title>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<center>
<div>
<%@ include file="top.html" %>
</div>
<h3>Struts 2文件上传</h3>
<hr/>
<s:form action="myUpload" enctype="multipart/form-data">
<s:file name="doc" label="选择上传文件"/>
<s:submit value="上传"/>
</s:form>
</center>
</body>
</html>
MyUpload.java
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyUpload extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 417923359216619076L;
// 封装上传文件域的属性
private File doc;
// 封装上传文件的类型
private String docContentType;
// 封装上传文件名
private String docFileName;
private String path; //接受依赖注入的属性
@SuppressWarnings("deprecation")
public String getPath() { //返回上传文件的保存位置
return ServletActionContext.getRequest().getRealPath(path);
}
public void setPath(String path) {
this.path = path;
}
public File getDoc() {
return doc;
}
public void setDoc(File doc) {
this.doc = doc;
}
public String getDocContentType() {
return docContentType;
}
public void setDocContentType(String docContentType) {
this.docContentType = docContentType;
}
public String getDocFileName() {
return docFileName;
}
public void setDocFileName(String docFileName) {
this.docFileName = docFileName;
}
@SuppressWarnings("resource")
@Override
public String execute() throws Exception {
docFileName = getFileName(docFileName);
FileOutputStream fos = new FileOutputStream(getPath() + "\\" + docFileName);
FileInputStream fis = new FileInputStream(doc);
byte[] b = new byte[1024];
int length = 0;
while((length = fis.read(b)) > 0) { //强文件上传的内容保存到服务器端
fos.write(b, 0, length);
}
return SUCCESS;
}
private String getFileName(String fileName) {
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
return System.currentTimeMillis() + extension;
}
}
上述的3个属性名称不能随意,如果file的标签不是doc,而是abc,那么Action累中封装的属性是abc,abccontextType和abcFileName.
配置struts,xml
<!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 name="struts.custom.i18n.resources" value="globalMessages" />
<package name="default" extends="struts-default">
<action name="myUpload" class="action.MyUpload">
<!-- 上传文件拦截,只能上传图片资源 -->
<interceptor-ref name="fileUpload">
<!--allowedTypes允许上传的文件类型 -->
<param name="allowedTypes">
image/x-png,image/gif,image/bmp,image/pjpeg
</param>
<!-- 上传文件的大小单位 字节 -->
<param name="maximumSize">30000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="path">/upload</param>
<result name="success">/uploadSuccess.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
建立uploadSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传成功</title>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<center>
<div>
<%@ include file="top.html" %>
</div>
<h3>文件上传成功</h3>
<hr/>
<img src="upload/<s:property value="docFileName"/>"/>
<
<br/>
<s:property value="docFileName"/>
</center>
</body>
</html>
在src目录下配置国际化显示信息
globalMessages_zh_CN.properties
struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u7C7B\u578B\u6709\u8BEF\uFF01\u4E0A\u4F20\u6587\u4EF6\u5FC5\u987B\u4E3A\u56FE\u7247
struts.messages.error.file.too.large=\u8BF7\u4F7F\u752830000\u5B57\u8282\u4EE5\u4E0B\u7684\u6587\u4EF6\uFF01
输入http://localhost:8080/Demo11/upload.jsp,选择一个图片。运行成功