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

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.xml
	private 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();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值