使用extjs上传文件到java后台的完整代码案例,本案例经过多次重复利用,贴出来,下次用的时候方便直接复制过去,也方便兄弟们。
1.extjs 弹出上传文件的窗口方法:
function showUploadWin(htmlText){ var uploadWin=Ext.getCmp("uploadFileWin"); if(!uploadWin){ uploadWin=new Ext.Window({ modal :true, title:"导入word文档", width:400, id:"uploadFileWin", height:230, bodyBorder:false, layout:"fit", bodyStyle:"background-color:#FFF", items:[{ border:false, id:"uploadForm", xtype:"form", fileUpload: true , style:"padding-top:50px;", items:[{ xtype:'textfield', inputType:'file', id:"uploadFileTextF", name:'uploadFile', labelStyle : 'text-align:right;font-weight:bold', allowBlank: false , fieldLabel:'选择文件*' }] }], buttonAlign:"center", buttons:[{ text:"确定", handler:function(){ var form=Ext.getCmp("uploadForm").form; var fileName=Ext.getCmp("uploadFileTextF").getValue(); fileName=fileName.split('.'); fileName=fileName[fileName.length-1]; if(fileName!="doc"&&fileName!="docx"){ Ext.Msg.alert("系统提示","必须选择Microsoft Office Word 文档!"); return false; } if (form.isValid()){ form.submit({ method:'post', url:'../WebManage?action=importWord', // 根据自己系统的需要调用程序处理上传文件 24 waitMsg:'文件上传中...', success: function (form,action) { if(Ext.getCmp("title").getValue()=="")Ext.getCmp("title").setValue(action.result.fileName); htmlText.html(action.result.msg); Ext.Msg.alert("系统提示", "文件导入成功!(注:如有个别图片样式问题请截图插入)"); Ext.getCmp("uploadFileWin").close(); }, failure: function (form,action){ Ext.Msg.alert("系统提示", "文件上传失败!"); } }); } else { Ext.Msg.alert("系统提示","请选择文件后再上传!"); } } },{ text:"关闭", handler:function(){ Ext.getCmp("uploadFileWin").close(); } }] }); } uploadWin.show(); }
2.java后台接收extjs上传的文件的代码
String action=request.getParameter("action"); if(action.equals("importWord")){ int maxPostSize = 1000 * 1024 * 1024; FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(factory); servletFileUpload.setSizeMax(maxPostSize); List fileItems = servletFileUpload.parseRequest(request); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) {// 是文件 String realPath=request.getSession().getServletContext().getRealPath(""); java.util.Date date=new java.util.Date(); String imgPath="/img/fileImg/"+date.getTime()+"/"; realPath=realPath+imgPath; String fileName=item.getName(); String returnWordStr=WordToHtml.convert2Html(item.getInputStream(),realPath,imgPath); PrintWriter out = response.getWriter(); returnWordStr = returnWordStr.replaceAll("\n", "").replaceAll("\r", ""); returnWordStr = returnWordStr.replaceAll( "'", "'"); //returnWordStr = returnWordStr.replaceAll( "&", "&"); returnWordStr = returnWordStr.replaceAll( "\"", ""); //" //returnWordStr = returnWordStr.replaceAll( "\t", " ");// 替换跳格 //returnWordStr = returnWordStr.replaceAll( " ", " ");// 替换空格 //returnWordStr = returnWordStr.replaceAll("<", "<"); //returnWordStr = returnWordStr.replaceAll( ">", ">"); out.print("{success:true,msg:'"+returnWordStr+"',fileName:'"+fileName+"'}"); out.flush(); out.close(); } } }