Extjs+struts实现文件上传
1.fileUpload.js
Ext.onReady(function(){
var form = new Ext.form.FormPanel({
renderTo:'file',
labelAlign: 'right',
title: '文件上传',
labelWidth: 60,
frame:true,
url: '../upload.do?op=uploadFile',//fileUploadServlet
width: 300,
height:200,
fileUpload: true,
items: [{
xtype: 'textfield',
fieldLabel: '文件名',
name: 'file',
inputType: 'file'//文件类型
}],
buttons: [{
text: '上传',
handler: function() {
form.getForm().submit({
success: function(form, action){
Ext.Msg.alert('信息', '文件上传成功!');
},
failure: function(){
Ext.Msg.alert('错误', '文件上传失败');
}
});
}
}]
});
});
2.uploadAction.java 其中使用了cos上传组件,下载地址:http://www.servlets.com/cos/cos-05Nov2002.zip
package zx.struts.actions;
import java.io.File;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.oreilly.servlet.MultipartRequest;
public class UploadAction extends DispatchAction{
public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
String saveDirectory ="F:\\jboss-4.2.2.GA\\server\\default\\deploy\\userDemo.war\\upload"; //文件上传后,保存的位置
int maxPostSize =3 * 5 * 1024 * 1024 ; //每个文件最大5MB,最多3个文件,所以...
//response的编码为"gb2312"
MultipartRequest multi =
new MultipartRequest(request, saveDirectory, maxPostSize,
"gb2312");
//输出反馈信息
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
String name = (String)files.nextElement();
File f = multi.getFile(name);
if(f!=null){
String fileName = multi.getFilesystemName(name);
String lastFileName= saveDirectory+"\\" + fileName;
System.out.println("上传的文件:"+lastFileName);
}
}
return null;
}
}