strus2 配置文件
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 UploadAction extends ActionSupport {
//封裝文件標題請求參數的屬性
private String title;
//封裝上傳文件域的屬性
private File[] upload;
//封裝上傳文件類型的屬性
private String[] uploadContentType;
//封裝上傳文件名的屬性
private String[] uploadFileName;
//直接在struts.xml文件中配置的屬性
private String savePath;
private String allowType;
//返回上傳文件的保存位置
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath);
}
//接受struts.xml文件配置值的方法
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String upload() throws Exception {
for(int i=0;i<upload.length;i++){
//以服務器的文件保存地址和原文件名建立上傳文件輸出流
FileInputStream fis = new FileInputStream(upload[i]);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getTitle()+uploadFileName[i]);
System.out.println(getUploadFileName()+"=================");
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;
}
//文件標題的setter和getter方法
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
//上傳文件對應文件內容的setter和getter方法
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
//上傳文件的文件類型的setter和getter方法
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
//上傳文件的文件名的setter和getter方法
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getAllowType() {
return allowType;
}
public void setAllowType(String allowType) {
this.allowType = allowType;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 國際化 -->
<constant name="struts.custom.i18n.resources" value="mess" />
<!-- 設置該應用使用的解碼集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="upload1" extends="struts-default">
<!-- 配置處理文件上傳的Action -->
<action name="upload" class="action.UploadAction" method="upload">
<!-- 動態設置Action的屬性值 -->
<param name="savePath">/uploadFiles</param>
<interceptor-ref name="fileUpload">
<!-- 配置允許上傳的文件類型 -->
<param name="allowedTypes">image/png,image/gif,image/jpeg,image/jpg</param>
<!-- 配置允許上傳的文件大小 -->
<param name="maximumSize">20000</param>
</interceptor-ref>
<!--配置內部的攔截器, -->
<interceptor-ref name="defaultStack" />
<!-- 配置Struts 2默認的視圖頁面 -->
<result>/succ.jsp</result>
<!-- 攔截器調用 -->
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
jsp 頁面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>簡單的文件上傳</title>
</head>
<body>
<s:fielderror/>
<s:form action="/upload" enctype="multipart/form-data">
<s:textfield name="title" label="文件標題"/>
<s:file name="upload" label="選擇文件"/>
<s:file name="upload" label="選擇文件"/>
<s:file name="upload" label="選擇文件"/>
<s:submit value="上傳"/>
</s:form>
</body>
</html>
succ.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上傳成功</title>
</head>
<body>
上傳成功<br/>
文件標題:<s:property value=" +title"/><br/>
文件爲: <img src="<s:property value=" 'uploadFiles/' +uploadFileName"/>"/><br/>
</body>
</html>
國際化:
mess.properties
#\u6539\u53D8\u6587\u4EF6\u7C7B\u578B\u4E0D\u5141\u8BB8\u7684\u63D0\u793A\u4FE1\u606F
struts.messages.error.content.type.not.allowed=\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u53EA\u80FD\u662F\u56FE\u7247\u6587\u4EF6\uFF01\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01
#\u6539\u53D8\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\u7684\u63D0\u793A\u4FE1\u606F
struts.messages.error.file.too.large=\u60A8\u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01