Ext + struts2 UploadDialog 文件上传

原文:http://hi.baidu.com/freespace520/blog/item/d789b40f2a6564226159f308.html/cmtid/54e0593169838aa35fdf0ed1
的插件部件,过滤下边的几种文件格式(jpg..)只有这几种格式才可以上传

//文件上传
var upload =new Ext.Button({
text:'文件上传',
iconCls:'icon-upload',
listeners:{
click:function(btnThis,eventobj){
if(fondsId!=''){
dialog = new Ext.ux.UploadDialog.Dialog({
autoCreate: true,
closable: true,
collapsible: false,
draggable: true,
minWidth: 400,
minHeight: 200,
width: 400,
height: 350,
permitted_extensions:

['JPG','jpg','jpeg','JPEG','GIF','gif','pdf','txt','zip','ZIP','doc','DOC'],
proxyDrag: true,
resizable: true,
constraintoviewport: true,
title: '文件上传',
url: '/as3/mount/upload_file.action',
reset_on_hide: false,
allow_close_on_upload: true
});
dialog.show();
}else{
Ext.Msg.alert('提示','请先选择一个***再上传文件');
}
}
}
});

struts2 action 部分

因为在EXT中已经过滤了格式,所以在annotation的allowedTypes中就不用再写了,此该再重新设置是为了如

果在JSP页面时上传文件时用.

@InterceptorRef(value="fileUpload")
为struts2 的上传过滤器,因为在struts2的默认配置文件中没有配置此过滤器,所以用到上传一定要自己手

动再配置上.
@InterceptorRef(value="defaultStack")
struts2 的默认过滤器栈,一定要配置在上传过滤器的后边

再看下action 中的三个属性, file ,fileFileName,fileContentType; 是一定要配置的三个属性:文件

,文件名,文件类型;
file 不能更改,
fileFileName 可以根据自己页面的属性名自己修改,但是一定要有fileName,比如JSP页面<input

type='text' name="abc" value="文件名">

那么在action 中定义文件名属性时一定要叫abcFileName;
fileFileContentType 雷同;

更多细节请查看 apache: common-fileupload 子项目


@InterceptorRefs({@InterceptorRef(value="fileUpload", params=

{"allowedTypes","image/bmp,image/PNG,image/gif,image/JPEG,image/JPG,image/jpg,application/x-

zip-compressed","maximumSize","20971520"}
),@InterceptorRef(value="defaultStack")})
@SuppressWarnings("serial")
public class UploadFileAction extends ActionSupport {

private File file;

private String fileFileName;

private String fileContentType;

public String getFileContentType() {
return fileContentType;
}

public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public void setFile(File file) {
this.file = file;
}

public File getFile(){
return file;
}

public String execute(){
System.out.println("begin upload file.... ");


LinkPath link = LinkPath.newInstance();
String dataPath = link.getdataPath();

DateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
Date date=new Date();
String dateDir = format.format(date);
File f = new File(dataPath+"\\"+dateDir);

if(!f.exists()){
f.mkdirs();
}

File dataFile = new File(dataPath+"\\"+dateDir+"\\"+this.getFileFileName());

try {
//将第一个参数对应的 文件 copy 到 第二个参数对应的文件中
FileUtil.copyFile(this.file,dataFile);


if(dataFile.exists()){

String fileType = dataFile.getPath().substring

(dataFile.getPath().lastIndexOf("."),dataFile.getPath().length());

if(".zip".equals(fileType)||".ZIP".equals(fileType)){
UpZIP zip = new UpZIP();

zip.unzip(dataFile.getPath(), dataPath+"\\"+dateDir);

dataFile.delete();
}
}

Struts2Utils.renderText("{success:true,message:'上传成功'}");
} catch (IOException e) {
Struts2Utils.renderText("{success:flase,message:'失败'}");
e.printStackTrace();
}
return null;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值