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();}}
- struts文件上传,并手动实现文件过滤
- struts--实现上传文件
- struts 实现文件上传
- struts 实现文件上传
- struts实现文件上传
- struts实现文件上传
- Struts实现文件上传。
- Struts 2实现文件过滤
- struts2的文件上传(2):手动实现上传文件类型的过滤
- struts2实现文件上传(类型、大小过滤——手动、拦截器)
- HDFS过滤合并并上传文件demo
- 用Struts实现文件上传
- struts上传文件的实现
- struts实现多文件上传
- struts 2实现文件上传
- struts实现文件上传程序
- Struts 2 实现上传文件
- struts实现upload文件上传
- hibernate学习笔记(乐观锁,悲观锁)
- nginx源码分析(2)——http模块的初始化过程
- 百年管理看流程之一序言
- 工作中遇到的问题(累计)
- The Psychology of Cross Country
- struts文件上传,并手动实现文件过滤
- 腾讯这么多年来,最大的失误在哪?
- android 调用系统图片浏览器并返回图片路径
- 我怎么了?
- 触摸屏代码分析——实现滑动功能
- ubuntu登陆root用户
- android 版本的天气预报
- hdu 3328 Flipper(栈)
- 调用android系统相机拍照并保存