日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

struts2+extjs文件上传完整实现(攻克了上传中的各种问题)

發布時間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts2+extjs文件上传完整实现(攻克了上传中的各种问题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權聲明:本文為博主原創文章。未經博主同意不得轉載。 https://blog.csdn.net/shanhuhau/article/details/28617999

首先須要引入上傳控件

<script type="text/javascript" src="<%=basePath%>/js/ext/examples/ux/fileuploadfield/FileUploadField.js" charset="utf-8"></script>

彈出上傳框相應extjs代碼

var uploadForm=new Ext.FormPanel({id:'uploadForm',width:520,frame:true,fileUpload: true, autoHeight:true,bodyStyle:'10px 10px 0px 10px',labelWidth:50,enctype: 'multipart/form-data', defaults:{anchor: '95%',allowBlank: false},items:[{xtype:'fileuploadfield',emptyText: '請選擇上傳文件...', fieldLabel: '文件:', id:'uploadFile',name: 'upload', allowBlank: false, blankText: '文件名不能為空.', buttonCfg: {text: '選擇...'// 上傳文件時的本地查找按鈕}}],buttons: [{text: '上傳',handler: function(){if(uploadForm.getForm().isValid()){uploadForm.getForm().submit({url:basePath+ '/documentManage/upload_upload.action',method:'POST',waitTitle: '請稍后',waitMsg: '正在上傳文檔文件 ...',success: function(fp, action){Ext.MessageBox.alert('信息', action.result.msg); Ext.getCmp("uploadFile").reset(); // 指定文件字段的id清空其內容addwin.hide();grid.store.load({params:{start : 0,limit : combo.value}});},failure: function(fp, action){Ext.MessageBox.alert('警告', action.result.msg); addwin.hide();}});}}},{text: '重置',handler: function(){uploadForm.getForm().reset();}}]});addwin = new Ext.Window({title : '上傳新文檔',closable : true,width : 520,autoHeight: true,border : false,plain : true,modal : true,layout : 'fit',bodyStyle : 'padding:5px;',maximizable : false,// 禁止最大化closeAction : 'hide',closable : true,// 是否有關閉collapsible : true,// 可折疊iconCls : 'bind',items : [uploadForm] });



struts2 action代碼

package cn.com.action;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import cn.com.css.common.action.BaseAction;public class FileUploadAction extends BaseAction {private static final long serialVersionUID = 5156288255337069381L;private String msg;private String contentType;private File docmentFile;private String fileName;public String upload() throws Exception {String realPath = "E:\\" + fileName;if (docmentFile.isFile()) {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(docmentFile));BufferedOutputStream bos = null;try {bos = new BufferedOutputStream(new FileOutputStream(realPath));// 為以防萬一,以后寫文件的路徑盡量寫成正雙斜杠的// 從源文件里取數據,寫到目標文件里byte[] buff = new byte[8192];for (int len = -1; (len = bis.read(buff)) != -1;) {bos.write(buff, 0, len);}bos.flush();} catch (IOException ie) {ie.printStackTrace();msg="文件上傳失敗";HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html;charset=UTF-8");return "none";} finally {if (bis != null) {try {bis.close();} catch (IOException ie) {ie.printStackTrace();}}if (bos != null) {try {bos.close();} catch (IOException ie) {ie.printStackTrace();}}}}msg="文件上傳成功";HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html;charset=UTF-8");return "none";}public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}// since we are using <s:file name="upload" .../> the file name will be// obtained through getter/setter of <file-tag-name>FileNamepublic String getUploadFileName() {return fileName;}public void setUploadFileName(String fileName) {this.fileName = fileName;}// since we are using <s:file name="upload" ... /> the content type will be// obtained through getter/setter of <file-tag-name>ContentTypepublic String getUploadContentType() {return contentType;}public void setUploadContentType(String contentType) {this.contentType = contentType;}// since we are using <s:file name="upload" ... /> the File itself will be// obtained through getter/setter of <file-tag-name>public File getUpload() {return docmentFile;}public void setUpload(File docmentFile) {this.docmentFile = docmentFile;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public String getContentType() {return contentType;}public void setContentType(String contentType) {this.contentType = contentType;}public File getDocmentFile() {return docmentFile;}public void setDocmentFile(File docmentFile) {this.docmentFile = docmentFile;}}


struts.xml配置:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><package name="documentManage" namespace="/documentManage"extends="global-struts-default"><action name="upload_*"class="cn.com.FileUploadAction"method="{1}"><result type="json" name="none"><param name="contentType">text/html;charset=utf-8</param><param name="excludeProperties">user.myQuestionses,user.messages,user.myNotes,user.taskPapers,user.tasks,user.testPapers,user.articles</param></result></action></package> </struts>


轉載于:https://www.cnblogs.com/mqxnongmin/p/10619014.html

總結

以上是生活随笔為你收集整理的struts2+extjs文件上传完整实现(攻克了上传中的各种问题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。