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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值