ExtJs3.2.2+Struts2实现文件的上传

Struts2支持Common-FileUpload框架和COS框架,不过Struts2又在这些上传项目的基础上做了封装,屏蔽了上传文件的代码编程差别。

var uploadForm = new Ext.form.FormPanel({
......
fileUpload : true,//注意这个必须的,否则文件内容并未上传上去
items:[{
xtype : 'textfield',
fieldLabel : '音源上传',
id : 'uploadFile',
name : 'uploadFile',
inputType : 'file',//指定是文件类型输入框
vtype : 'fileType',
allowBlank : false
}]
......
})


SongAction中定义三个属性用来存储文件上传的信息
private File uploadFile;//音源文件(必须,用于存放上传文件的内容)
private String uploadFileFileName;//音源文件名(可选,存放上传文件的文件名,不包含路径)
private String uploadFileContentType;//音源的类型(可选)

注意:三个属性的命名是有规定的,如果表单的文件域的id命名为xxx,那么上面三个属性的命名规则如下:
private File xxx;
private String xxxFileName;
private String xxxContentType;


保存文件:
private static void copy(File src, File dst) {
//TODO
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}


有空的话,欢迎到[url=http://hongfanshu.blogbus.com/]红番薯[/url]逛逛
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值