struts文件上传,并手动实现文件过滤

来源:互联网 发布:冰川网络最新手游 编辑:程序博客网 时间:2024/05/05 22:17

struts.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>    <package name="file-upload" namespace="/fileupload" extends="struts-default">    <action name="fileupload" class="fileupload.UploadAction">    <result name="success">success.jsp</result>    <result name="type">type.jsp</result>    <param name="savePath">F:/</param>    <param name="allowTypes">txt,exe</param>    </action>    </package>    </struts>

Action:

package fileupload;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class UploadAction {private String title;private File upload; // 要上传的文件<input type="file" name="upload" />private String uploadFileName; // 得到上传文件的名字private String uploadContentType; // 前面三个是 struts 自动封闭好的private String savePath; // <param name="savePath">F:/</param> 设置文件保存路径 在// struts.xmlprivate String allowTypes; // <param name="allowTypes">txt,exe</param>// 设置文件的类型/* * getter setter */public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}/* * getter setter */public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}/* * getter setter */public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}/* * getter setter */public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}/* * getter setter */public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}/* * getter setter */public String getAllowTypes() {return allowTypes;}public void setAllowTypes(String allowTypes) {this.allowTypes = allowTypes;}// 返回struts.xml 配置的允许的文件类型,返回 <param name="allowTypes">txt,exe</param>// 这里面的类型private String[] getTypes() {String stringType = getAllowTypes();String[] types = stringType.split(",");return types;}/* * 检查提交的文件类型是否符合上传 要求 ,调用 getTypes()得到所有的类型 */private boolean is_filter() {String args[] = getTypes();String name = getUploadFileName();String type = name.substring(name.lastIndexOf(".") + 1, name.length());System.out.println(type);for (String s : args)if (s.equals(type))return true;return false;}/* * & 上传文件 */private String doSave() throws Exception {if (!is_filter())return "type";FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"+ getUploadFileName());FileInputStream fin = new FileInputStream(getUpload());byte[] buffer = new byte[1024 * 1024];int length = -1;while ((length = fin.read(buffer)) != -1) {fos.write(buffer, 0, length);}return "success";}public String execute() throws Exception {/* * System.out.println(uploadFileName); System.out.println(upload); * System.out.println(uploadContentType); System.out.println(savePath); */return doSave();}}


原创粉丝点击