首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2下传文件手动实现文件过滤

2012-10-17 
Struts2上传文件手动实现文件过滤在实现了上传的功能的情况下,再加一点点代码就可以实现手动文件类型过滤

Struts2上传文件手动实现文件过滤
在实现了上传的功能的情况下,再加一点点代码就可以实现手动文件类型过滤


客户端jsp请求

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>简单的文件上传</title><meta name="author" content="Yeeku.H.Lee" /><meta name="website" content="http://www.crazyit.org" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body><!--  <span style="color: red">${requestScope.typeError}</span> --><s:debug></s:debug><span style="color: red"> <s:property value="#typeError" /> </span><form action="upload.action" method="post"enctype="multipart/form-data">文件标题:<input type="text" name="title" /><br />选择文件:<input type="file" name="upload" /><br /><input value="上传" type="submit" /></form></body></html>
?


Action中部分代码
package lee;/** * 手动实现文件过滤 */import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UploadAction extends ActionSupport implements ServletContextAware {//封装文件标题请求参数的属性private String title;//封装上传文件域的属性private File upload;//封装上传文件类型的属性private String uploadContentType;//封装上传文件名的属性private String uploadFileName;//定义Action允许过滤的文件类型private String allowTypes;private ServletContext context;@Overridepublic String execute() throws Exception{//将允许上传文件类型的字符串以英文(,)分解成字符串数组//从而判断当前文件类型是否可以上传String filterResult = filterType(getAllowTypes().split(","));//System.out.println(getAllowTypes());//System.out.println( filterResult);if(filterResult != null){//此put方法是将值放在Stack Content中而不是Value Stack Contents中ActionContext.getContext().put("typeError", "你要上传的文件类型不正确!!!");return filterResult;}//以服务器的文件保存地址和原文件名建立上传文件输出流FileOutputStream fos = new FileOutputStream(context.getRealPath("/upload")+ "\" + getUploadFileName());FileInputStream fis = new FileInputStream(getUpload());byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0){fos.write(buffer , 0 , len);}return SUCCESS;}//此方法用于过滤文件类型public String filterType(String[] types){//获取希望上传的文件类型String fileType = getUploadContentType();for (String type : types) {if(type.equals(fileType)){return null;}}return INPUT;}//文件标题的setter和getter方法public void setTitle(String title) {this.title = title; }public String getTitle(){return (this.title); }//上传文件对应文件内容的setter和getter方法public void setUpload(File upload) {this.upload = upload; }public File getUpload() {return (this.upload); }//上传文件的文件类型的setter和getter方法public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType; }public String getUploadContentType(){return (this.uploadContentType); }//上传文件的文件名的setter和getter方法public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName; }public String getUploadFileName() {return (this.uploadFileName); }public String getAllowTypes() {return allowTypes;}public void setAllowTypes(String allowTypes) {this.allowTypes = allowTypes;}public ServletContext getContext() {return context;}public void setContext(ServletContext context) {this.context = context;}public void setServletContext(ServletContext context) {this.context = context;}}
?
Action只自己多一个方法要与过滤文件类型
//此方法用于过滤文件类型public String filterType(String[] types){//获取希望上传的文件类型String fileType = getUploadContentType();for (String type : types) {if(type.equals(fileType)){return null;}}return INPUT;}
?


strut. xml文件(多设置一个参数用于限定文件的类型Action中直接取)
<?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><!-- 指定国际化资源文件的baseName为globalMessages --><constant name="struts.custom.i18n.resources" value="globalMessages"/><!-- 设置该应用使用的解码集 --><constant name="struts.i18n.encoding" value="UTF-8"/><package name="lee" extends="struts-default"><!-- 配置处理文件上传的Action --><action name="upload" class="lee.UploadAction"><!-- 设置允许上传文件的文件类型 --><param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param><!-- 上传失败退回input逻辑视图 --><result name="input">/upload.jsp</result><!-- 配置Struts 2默认的视图页面 --><result>/succ.jsp</result></action></package></struts>
?





?

热点排行