日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

struts2中实现文件的上传

發(fā)布時(shí)間:2024/8/23 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts2中实现文件的上传 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
struts2中實(shí)現(xiàn)文件的上傳

文件上傳的action,同時(shí)過(guò)濾上傳的文件格式只對(duì)滿足要求的格式支持上傳

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文件中提供上傳文件保存路徑的動(dòng)態(tài)配置和允許的文件上傳格式的動(dòng)態(tài)配置

<?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>

上傳成功的轉(zhuǎn)向頁(yè)面:

<body>
上傳的文件標(biāo)題是:<s:property value="title"/><br/>
上傳的圖片是: <img alt="upload" src="<s:property value="'upload/'+uploadFileName"/>"/>
</body>

文件上傳頁(yè)面jsp文件:

<body>
${requestScope.typeError }
<form action="upload.action" method="post" name="uploadForm" enctype="multipart/form-data">
文件標(biāo)題:<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類與文件的上傳無(wú)關(guān),但是在文件上傳的應(yīng)用中會(huì)出現(xiàn)無(wú)法預(yù)測(cè)的異常,添加該過(guò)濾器后異常消失。本身org.apache.struts2.dispatcher.ActionContextCleanUp類就是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter類的輔助類,此處只是出于系統(tǒng)穩(wěn)定性的考慮,與應(yīng)用的文件上傳功能沒(méi)有直接的關(guān)系。

注意:struts2的文件上傳默認(rèn)使用的是common-fileupload和common-io文件上傳 框架,所以需要導(dǎo)入對(duì)應(yīng)的jar包,另外保存上傳文件的文件夾應(yīng)該是在webRoot目錄下,而不是在src目錄下,經(jīng)過(guò)編譯后部署到tomcat中的程序只是webRoot中的經(jīng)過(guò)編譯的class文件和所有的jsp,jar,properties,xml等文件而不會(huì)包含src文件,所以應(yīng)該將upload文件夾建在webRoot目錄下。

posted on 2013-04-16 22:37 moonfans 閱讀(...) 評(píng)論(...) 編輯 收藏

轉(zhuǎn)載于:https://www.cnblogs.com/moonfans/archive/2013/04/16/3025186.html

總結(jié)

以上是生活随笔為你收集整理的struts2中实现文件的上传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。