ExtJs + Struts2文件上传

ExtJs表单代码: 

Java代码 
  1. Ext.onReady(function() {  
  2.     //定义数据集对象  
  3.     var mystore = new Ext.data.Store({  
  4.         autoLoad : true,  
  5.         sortInfo : {  
  6.             field : "uploadtime",  
  7.             direction : "DESC"  
  8.         },  
  9.         reader : new Ext.data.JsonReader({  
  10.             totalRecords : "results",  
  11.             root : "dataList",  
  12.             id : 'id',  
  13.             fields : [{  
  14.                 name : 'id'  
  15.             }, {  
  16.                 name : 'realName'  
  17.             }, {  
  18.                 name : 'contentType'  
  19.             }, {  
  20.                 name : 'size'  
  21.             }, {  
  22.                 name : 'uploadtime',  
  23.                 sortable : true  
  24.             }]  
  25.         }),  
  26.         proxy : new Ext.data.HttpProxy({  
  27.             url : 'file_getAll.action',  
  28.             method : 'POST'  
  29.         })  
  30.     })  
  31.     //创建工具栏组件  
  32.     var toolbar = new Ext.Toolbar([{  
  33.         text : '上传文件',  
  34.         iconCls : 'add',  
  35.         handler : showAdd  
  36.     },  
  37.             //          {text : '修改信息',iconCls:'option',handler:showModify},  
  38.             {  
  39.                 text : '删除文件',  
  40.                 iconCls : 'remove',  
  41.                 handler : showDelete  
  42.             }]);  
  43.     //创建Grid表格组件  
  44.     var cb = new Ext.grid.CheckboxSelectionModel()  
  45.     var myGrid = new Ext.grid.GridPanel({  
  46.         applyTo : 'grid-div',  
  47.         frame : true,  
  48.         tbar : toolbar,  
  49.         store : mystore,  
  50.         stripeRows : true,  
  51.         autoScroll : true,  
  52.         viewConfig : {  
  53.             autoFill : true  
  54.         },  
  55.         sm : cb,  
  56.         columns : [//配置表格列  
  57.         new Ext.grid.RowNumberer({  
  58.             header : '行号',  
  59.             width : 40  
  60.         }),     //表格行号组件  
  61.                 cb, {  
  62.                     header : "文件编号",  
  63.                     width : 40,  
  64.                     dataIndex : 'id',  
  65.                     sortable : true  
  66.                 }, {  
  67.                     header : "文件名",  
  68.                     width : 100,  
  69.                     dataIndex : 'realName',  
  70.                     sortable : true  
  71.                 }, {  
  72.                     header : "文件类型",  
  73.                     width : 50,  
  74.                     dataIndex : 'contentType',  
  75.                     sortable : true  
  76.                 }, {  
  77.                     header : "文件大小",  
  78.                     width : 50,  
  79.                     dataIndex : 'size',  
  80.                     sortable : true  
  81.                 }, {  
  82.                     header : "发布时间",  
  83.                     width : 80,  
  84.                     dataIndex : 'uploadtime',  
  85.                     sortable : true  
  86.                 }]  
  87.     })  
  88.     //创建新增或修改通告信息的form表单  
  89.     Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); // 设置缓存  
  90.     Ext.QuickTips.init();  
  91.     Ext.form.Field.prototype.msgTarget = 'side';//统一指定错误信息提示方式  
  92.   
  93.     var myForm = new Ext.form.FormPanel({  
  94.         width : 380,  
  95.         height : 200,  
  96.         frame : true,  
  97.         monitorValid:true,  
  98.         fileUpload:true//需上传文件  
  99.         url : 'file_upload.action',//请求的url地址  
  100.         method:'POST',  
  101.         items : [{  
  102.             xtype : 'textfield',  
  103.             name : 'realname',  
  104.             fieldLabel : '文件备注',  
  105.             allowBlank:false,  
  106.             emptyText:'请输入文件的文件名或其内容关键字……',  
  107.             blankText:'文件备注不能为空!'  
  108.         }, {  
  109.             xtype:'textfield',  
  110.             fieldLabel : '选择文件',  
  111.             name : 'attachment',  
  112.             //关键  
  113.             inputType : 'file',  
  114.             allowBlank:false,  
  115.             blankText:'请选择文件!',  
  116.             emptyText:'请选择上传文件'  
  117.         }],  
  118.         buttons : [{  
  119.             formBind:true,  
  120.             text : '上传',  
  121.             handler:function(){  
  122.                 myForm.form.submit({  
  123.                     clientValidation : true,//进行客户端验证  
  124.                     waitMsg : '正在上传文件,请稍等……',//提示信息  
  125.                     waitTitle : '提示',//标题  
  126.                     success : function(form, action) {//加载成功的处理函数  
  127.                         win.hide();  
  128.                         myGrid.getStore().reload();  
  129.                         Ext.Msg.alert('提示''上传成功');  
  130.                     },  
  131.                     failure : function(form, action) {//加载失败的处理函数  
  132.                         Ext.Msg.alert('提示''上传失败');  
  133.                     }  
  134.                 });  
  135.                 }  
  136.         }, {  
  137.             text : '关闭',  
  138.             handler:function(){  
  139.                 win.hide();  
  140.             }  
  141.         }]  
  142.     });  
  143.     //创建弹出窗口  
  144.     var win = new Ext.Window({  
  145.         layout : 'fit',  
  146.         width : 380//380  
  147.         closeAction : 'hide',  
  148.         height : 200//280  
  149.         resizable : false,  
  150.         shadow : true,  
  151.         modal : true,  
  152.         closable : true,  
  153.         bodyStyle : 'padding:5 5 5 5',  
  154.         animCollapse : true,  
  155.         items : [myForm]  
  156.     });  
  157.     //显示上传窗口  
  158.     function showAdd() {  
  159.         myForm.form.reset();  
  160.         myForm.isAdd = true;  
  161.         win.setTitle("文件上传");  
  162.         win.show();  
  163.     }  
  164.     //显示删除对话框  
  165.     function showDelete() {  
  166.         var IdList = geIdList();  
  167.         var num = IdList.length;  
  168.         if (num == 0) {  
  169.             return;  
  170.         }  
  171.         Ext.MessageBox.confirm("提示""您确定要删除所选文件吗?", function(btnId) {  
  172.             if (btnId == 'yes') {  
  173.                 Delete(IdList);  
  174.             }  
  175.         })  
  176.     }  
  177.     //删除信息  
  178.     function Delete(IdList) {  
  179.         var fileIds = IdList;  
  180.         var msgTip = Ext.MessageBox.show({  
  181.             title : '提示',  
  182.             width : 250,  
  183.             msg : '正在删除文件请稍后......'  
  184.         });  
  185.         Ext.Ajax.request({  
  186.             url : 'file_del.action',  
  187.             params : {  
  188.                 fileIds : fileIds  
  189.             },  
  190.             method : 'POST',  
  191.             success : function(response, options) {  
  192.                 msgTip.hide();  
  193.                 var result = Ext.util.JSON.decode(response.responseText);  
  194.                 if (result.success) {  
  195.                     //服务器端数据成功删除后,同步删除客户端列表中的数据  
  196.                     for (var i = 0; i < IdList.length; i++) {  
  197.                         var index = mystore.find('id', IdList[i]);  
  198.                         if (index != -1) {  
  199.                             var rec = mystore.getAt(index)  
  200.                             mystore.remove(rec);  
  201.                         }  
  202.                     }  
  203.                     Ext.Msg.alert('提示''删除文件成功。');  
  204.                 } else {  
  205.                     Ext.Msg.alert('提示''删除文件失败!');  
  206.                 }  
  207.             },  
  208.             failure : function(response, options) {  
  209.                 msgTip.hide();  
  210.                 Ext.Msg.alert('提示''删除文件请求失败!');  
  211.             }  
  212.         });  
  213.     }  
  214.     //数据修改后,同步更新信息列表信息  
  215.     function updateList(InfoId) {  
  216.         var fields = getFormFieldsObj(InfoId);  
  217.         var index = mystore.find('id', fields.id);  
  218.         if (index != -1) {  
  219.             var item = mystore.getAt(index);  
  220.             for (var fieldName in fields) {  
  221.                 item.set(fieldName, fields[fieldName]);  
  222.             }  
  223.             mystore.commitChanges();  
  224.         }  
  225.     }  
  226.   
  227.     //取得表单数据  
  228.     function getFormFieldsObj(InfoId) {  
  229.         var fields = myForm.items;  
  230.         var obj = {};  
  231.         for (var i = 0; i < fields.length; i++) {  
  232.             var item = fields.itemAt(i);  
  233.             var value = item.getValue();  
  234.             obj[item.name] = value;  
  235.         }  
  236.         if (Ext.isEmpty(obj['id'])) {  
  237.             obj['id'] = InfoId;  
  238.         }  
  239.         return obj;  
  240.     }  
  241.   
  242.     //取得所选信息  
  243.     function geIdList() {  
  244.         var recs = myGrid.getSelectionModel().getSelections();  
  245.         var list = [];  
  246.         if (recs.length == 0) {  
  247.             Ext.MessageBox.alert('提示''请选择要进行操作的文件!');  
  248.         } else {  
  249.             for (var i = 0; i < recs.length; i++) {  
  250.                 var rec = recs[i];  
  251.                 list.push(rec.get('id'));  
  252.             }  
  253.         }  
  254.         return list;  
  255.     }  
  256. });  

Acion代码: 
Java代码 
  1. package com.jyxx.action;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.commons.io.FileUtils;  
  8. import org.apache.struts2.ServletActionContext;  
  9. import org.apache.struts2.json.annotations.JSON;  
  10.   
  11. import com.jyxx.model.Ufile;  
  12. import com.jyxx.serviceimpl.fileServiceImpl;  
  13. import com.jyxx.utils.GetDate;  
  14. import com.jyxx.utils.UploadUtil;  
  15. import com.opensymphony.xwork2.ActionSupport;  
  16.   
  17. public class FileAction extends ActionSupport {  
  18.     /** 
  19.      *  
  20.      */  
  21.     private static final long serialVersionUID = 1L;  
  22.     private File attachment;  
  23.     private String fileName;  
  24.     private String contentType;  
  25.     private String size;  
  26.     private String realname;  
  27.     private String[] fileIds;  
  28.     private boolean flag = false;  
  29.     private int results;  
  30.     private List<Ufile> dataList = new ArrayList<Ufile>();  
  31.     private fileServiceImpl impl = new fileServiceImpl();  
  32.       
  33.       
  34.     public String getAll(){  
  35.         dataList = impl.getAllFiles();  
  36.         results = dataList.size();  
  37.         return SUCCESS;  
  38.     }  
  39.       
  40.     @SuppressWarnings("deprecation")  
  41.     public String upload() throws Exception {  
  42.         String realPath = ServletActionContext.getRequest().getRealPath(  
  43.                 "/upload");  
  44.         Ufile afile = new Ufile();  
  45.         String tname = UploadUtil.generateFileName(this.fileName);  
  46.         String tdir = realPath + "//" + tname;  
  47.         long s = this.attachment.length();  
  48.         if((1024 * 1024 * 1024) <= s){  
  49.             size = s /(1024 * 1024 * 1024) + "GB";  
  50.         }else if((1024 * 1024) <= s){  
  51.             size = s /(1024 * 1024) + "MB";  
  52.         }else if(1024 <= s){  
  53.             size = s /1024 + "KB";  
  54.         }else{  
  55.             size = s + "B";  
  56.         }  
  57.         File targetfile = new File(realPath, tname);  
  58.         FileUtils.copyFile(attachment, targetfile);  
  59.         afile.setFileName(tdir);  
  60.         afile.setContentType(this.contentType);  
  61.         afile.setRealName(realname);  
  62.         afile.setSize(size);  
  63.         GetDate date = new GetDate();  
  64.         afile.setUploadtime(date.getDate());  
  65.         this.flag = impl.add(afile);  
  66.         return NONE;  
  67.     }  
  68.       
  69.     public String del(){  
  70.         for(int i=0;i<fileIds.length;i++){  
  71.             int id = Integer.parseInt(fileIds[i]);  
  72.             File tem_file = new File(impl.getOne(id).getFileName());  
  73.             tem_file.delete();  
  74.             flag = impl.del(id);  
  75.         }  
  76.         return SUCCESS;  
  77.     }  
  78.   
  79.     public String getSize() {  
  80.         return size;  
  81.     }  
  82.   
  83.     public void setSize(String size) {  
  84.         this.size = size;  
  85.     }  
  86.   
  87.     public String getRealname() {  
  88.         return realname;  
  89.     }  
  90.   
  91.     public void setRealname(String realname) {  
  92.         this.realname = realname;  
  93.     }  
  94.   
  95.     public String[] getFileIds() {  
  96.         return fileIds;  
  97.     }  
  98.   
  99.     public void setFileIds(String[] fileIds) {  
  100.         this.fileIds = fileIds;  
  101.     }  
  102.       
  103.     @JSON(name="success")  
  104.     public boolean isFlag() {  
  105.         return flag;  
  106.     }  
  107.       
  108.     public void setFlag(boolean flag) {  
  109.         this.flag = flag;  
  110.     }  
  111.   
  112.     public int getResults() {  
  113.         return results;  
  114.     }  
  115.   
  116.     public void setResults(int results) {  
  117.         this.results = results;  
  118.     }  
  119.       
  120.     public String getFileName() {  
  121.         return fileName;  
  122.     }  
  123.   
  124.     public void setFileName(String fileName) {  
  125.         this.fileName = fileName;  
  126.     }  
  127.   
  128.     public String getContentType() {  
  129.         return contentType;  
  130.     }  
  131.   
  132.     public void setContentType(String contentType) {  
  133.         this.contentType = contentType;  
  134.     }  
  135.   
  136.     public void setAttachment(File attachment) {  
  137.         this.attachment = attachment;  
  138.     }  
  139.   
  140.     public void setAttachmentFileName(String attachmentFileName) {  
  141.         this.setFileName(attachmentFileName);  
  142.     }  
  143.   
  144.     public void setAttachmentContentType(String attachmentContentType) {  
  145.         this.setContentType(attachmentContentType);  
  146.     }  
  147.   
  148.     public List<Ufile> getDataList() {  
  149.         return dataList;  
  150.     }  
  151.   
  152.     public void setDataList(List<Ufile> dataList) {  
  153.         this.dataList = dataList;  
  154.     }  
  155.   
  156.       
  157.   
  158. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值