原理:struts2擁有自動轉換型別的功能,這是好處一。struts2我們所拷入的common-io.jar包整合了一個工具類,運用這個工具類可以幫我們達到上傳檔案的目的。
*首先搭建好struts2的環境(7個jar包,攔截器之類的配置),詳情可看:http://blog.csdn.net/mr_li13/article/details/49391329?
案例:運用struts2肯定要用它的配置檔案struts.xml啦、
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE struts PUBLIC
-
"-//Apache Software Foundation//DTD Struts Configuration <span style="color:#ff0000;">2.0</span>//EN"
-
"http://struts.apache.org/dtds/struts-<span style="color:#ff0000;">2.0</span>.dtd">
-
-
<struts>
-
-
<constant name="struts.action.extension" value="action,,do"></constant>
-
<constant name="struts.devMode" value="true"></constant>
-
-
-
-
-
<include file="UpLoadFile.xml"></include>
-
-
-
<include file="moreUpLoadFile.xml"></include>
-
-
-
</struts>
-
紅色標記處。一定要相同。不然就會出不聯網就會報錯。詳情請看
1.單個檔案:
UpLoadFile.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.action.extension" value="action,,do"></constant>
-
<constant name="struts.devMode" value="true"></constant>
-
-
<constant name="struts.multipart.maxSize" value="52428800"></constant>
-
-
-
<package name="upload" namespace="/upload" extends="struts-default">
-
<action name="upload1" class="com.itcast.web.domain.uploadAction" method="execute">
-
<result name="success">/uploadSuccess.jsp</result>
-
</action>
-
</package>
-
-
</struts>
上傳頁面:upload.jsp
-
-
<form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">
-
檔案:<input type="file" name="img"/><br/>
-
<input type="submit" value="上傳"/>
-
</form>
action實體類:uploadAction.class
-
package com.itcast.web.domain;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
import java.io.Serializable;
-
-
import javax.servlet.ServletContext;
-
-
import org.apache.commons.io.FileUtils;
-
import org.apache.struts2.ServletActionContext;
-
-
import com.opensymphony.xwork2.ActionContext;
-
import com.opensymphony.xwork2.ActionSupport;
-
-
public class uploadAction extends ActionSupport implements Serializable {
-
private File img;
-
private String imgFileName;
-
-
-
private String imgContentType;
-
-
-
-
-
-
-
-
-
-
public File getImg() {
-
return img;
-
}
-
-
public void setImg(File img) {
-
this.img = img;
-
}
-
-
-
-
public String getImgFileName() {
-
return imgFileName;
-
}
-
-
public void setImgFileName(String imgFileName) {
-
this.imgFileName = imgFileName;
-
}
-
-
public String getImgContentType() {
-
return imgContentType;
-
}
-
-
public void setImgContentType(String imgContentType) {
-
this.imgContentType = imgContentType;
-
}
-
-
-
-
-
-
-
public String execute(){
-
-
try {
-
-
System.out.println(imgContentType);
-
-
-
ServletContext sc=ServletActionContext.getServletContext();
-
String storpath=sc.getRealPath("files");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
FileUtils.copyFile(img, new File(storpath, imgFileName));
-
-
ActionContext.getContext().put("message", "上傳成功!");
-
return SUCCESS;
-
-
} catch (Exception e) {
-
e.printStackTrace();
-
return ERROR;
-
}
-
}
-
}
成功頁面:uploadSuccess.jsp
-
<body>
-
${message }
-
</body>
2.多檔案上傳
其實方式都差不多隻是將實體類中的屬性變成了陣列形式
moreUploadFile.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.action.extension" value="action,,do"></constant>
-
<constant name="struts.devMode" value="true"></constant>
-
-
<constant name="struts.multipart.maxSize" value="52428800"></constant>
-
<package name="moreupload" namespace="/moreupload" extends="struts-default">
-
<action name="upload1" class="com.itcast.web.domain.moreUploadAction" method="execute">
-
<result name="success">/uploadSuccess.jsp</result>
-
</action>
-
</package>
-
</struts>
moreupload.jsp
-
-
<form action="${pageContext.request.contextPath}/moreupload/upload1.action" method="post" enctype="multipart/form-data">
-
檔案1:<input type="file" name="img"/><br/>
-
檔案2:<input type="file" name="img"/><br/>
-
<input type="submit" value="上傳"/>
-
</form>
實體類:moreUploadAction.class
-
package com.itcast.web.domain;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.FileOutputStream;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
import java.io.Serializable;
-
-
import javax.servlet.ServletContext;
-
-
import org.apache.commons.io.FileUtils;
-
import org.apache.struts2.ServletActionContext;
-
-
import com.opensymphony.xwork2.ActionContext;
-
import com.opensymphony.xwork2.ActionSupport;
-
-
-
-
-
-
public class moreUploadAction extends ActionSupport implements Serializable {
-
private File[] img;
-
private String[] imgFileName;
-
-
-
private String[] imgContentType;
-
-
-
-
-
public File[] getImg() {
-
return img;
-
}
-
-
public void setImg(File[] img) {
-
this.img = img;
-
}
-
-
public String[] getImgFileName() {
-
return imgFileName;
-
}
-
-
public void setImgFileName(String[] imgFileName) {
-
this.imgFileName = imgFileName;
-
}
-
-
public String[] getImgContentType() {
-
return imgContentType;
-
}
-
-
public void setImgContentType(String[] imgContentType) {
-
this.imgContentType = imgContentType;
-
}
-
-
public String execute(){
-
try {
-
if(img!=null&&img.length>0){
-
ServletContext sc=ServletActionContext.getServletContext();
-
String storpath=sc.getRealPath("files");
-
-
for(int i=0;i<img.length;i++){
-
FileUtils.copyFile(img[i], new File(storpath, imgFileName[i]));
-
}
-
}
-
-
ActionContext.getContext().put("message", "上傳成功!");
-
return SUCCESS;
-
-
} catch (Exception e) {
-
e.printStackTrace();
-
return ERROR;
-
}
-
}
-
}
成功頁面和uploadSuccess.jso內容一樣的,共用。