struts2中实现文件的上传
文件上傳的action,同時過濾上傳的文件格式只對滿足要求的格式支持上傳
package com.inspur.action;
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 uploadFileName;
private String uploadContentType;
private String savePath;
private String allowTypes;
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String filterType(String[]types){
String fileType=this.getUploadContentType();
for(String type:types){
if(type.equals(fileType)){
return null;
}
}
return INPUT;
}
public String upload()throws Exception{
if(this.getAllowTypes()==null){
System.out.println("lzhq");
}
String filterResult=this.filterType(this.getAllowTypes().split(","));
if(filterResult!=null){
ActionContext.getContext().put("typeError", "this file you want to upload is error");
return filterResult;
}
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
FileInputStream fis=new FileInputStream(this.getUpload());
byte[]buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
}
在struts.xml文件中提供上傳文件保存路徑的動態配置和允許的文件上傳格式的動態配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<package name="upload" extends="struts-default" namespace="/">
<action name="upload" class="com.inspur.action.UploadAction" method="upload">
<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<param name="savePath">/upload</param>
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
上傳成功的轉向頁面:
<body>
上傳的文件標題是:<s:property value="title"/><br/>
上傳的圖片是: <img alt="upload" src="<s:property value="'upload/'+uploadFileName"/>"/>
</body>
文件上傳頁面jsp文件:
<body>
${requestScope.typeError }
<form action="upload.action" method="post" name="uploadForm" enctype="multipart/form-data">
文件標題:<input type="text" name="title"/><br/>
選擇文件:<input type="file" name="upload"/><br/>
<input type="submit" value="upload"/>
</form>
</body>
在web.xml配置文件中添加如下代碼:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中org.apache.struts2.dispatcher.ActionContextCleanUp類與文件的上傳無關,但是在文件上傳的應用中會出現無法預測的異常,添加該過濾器后異常消失。本身org.apache.struts2.dispatcher.ActionContextCleanUp類就是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter類的輔助類,此處只是出于系統穩定性的考慮,與應用的文件上傳功能沒有直接的關系。
注意:struts2的文件上傳默認使用的是common-fileupload和common-io文件上傳 框架,所以需要導入對應的jar包,另外保存上傳文件的文件夾應該是在webRoot目錄下,而不是在src目錄下,經過編譯后部署到tomcat中的程序只是webRoot中的經過編譯的class文件和所有的jsp,jar,properties,xml等文件而不會包含src文件,所以應該將upload文件夾建在webRoot目錄下。
posted on 2013-04-16 22:37 moonfans 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/moonfans/archive/2013/04/16/3025186.html
總結
以上是生活随笔為你收集整理的struts2中实现文件的上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二章 选择符和属性
- 下一篇: 如何让listView加入的Header