您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Java中利用dropzone.js實(shí)現(xiàn)一個(gè)文件拖拽上傳功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
jsp頁(yè)面:
1. 首先必須引入dropzone的js和css文件
<link rel="stylesheet" href="dropzone/css/dropzone.css" rel="external nofollow" > <script src="dropzone/js/dropzone.js"></script>
2.自己定義兩個(gè)div區(qū)域
<%--拖拽文件上傳 --%> <div id="div1" class="dropz" > uopload </div> <div id="div2" class="dropz" > </div>
這是我的文件上傳之后的文件隊(duì)列區(qū)域:
<div id="fileslist" ></div>
3.對(duì)dropzone.css進(jìn)行修改,將文件內(nèi)的所有dropzone替換為dropz
修改文件拖拽區(qū)域的顯示樣式:
.dropz {/*設(shè)置拖拽上傳文件按鈕的格式*/ min-height:0px; min-width: 100px; border: 1px solid #58AF0C; background: white; padding: 15px 20px; background-color: #7AC143; background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #7AC143), color-stop(1, #7AC143)); background-position: center top; background-repeat: no-repeat; border-radius: 5px; min-height:0px; min-width: 100px; padding: 15px 20px; color: #FFF; font: bold 12px Arial, Helvetica, sans-serif; text-align: center; text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); } .dropz.dz-clickable { cursor: pointer; line-height: 0px;/*按鈕中的文字垂直居中*/ }
4.在jsp對(duì)div進(jìn)行dropzone參數(shù)的自定義
<script type="text/javascript"> $("#div1").dropzone({ url:"systemController.action?saveFile",//上傳文件的地址, maxFiles:1,//最多上傳幾個(gè)文件 maxFilesize: 5,//文件的大小,單位是M addRemoveLinks:true,//是否有刪除文件的功能 dictRemoveFile:"",//刪除文件 previewsContainer:"#div2",//文件上傳進(jìn)度顯示的區(qū)域 acceptedFiles: ".jpg,.jpeg,.png,.gif,.xls,.txt,.sql,.rar,.mkv",//支持的格式 paramName:'file',//上傳的FILE名稱,即服務(wù)端可以通過(guò)此來(lái)獲取上傳的文件,如$_FILES['dropimage'] init: function() {//初始化時(shí)的事件 //$("#uploadfile").uploadFile({success:function(data){ this.on("addedfile", function(file) { // Create the remove button var removeButton = Dropzone.createElement("<img src='plug-in/uploadify/img/uploadify-cancel.png' title='刪除'/>"); // Capture the Dropzone instance as closure. var _this = this; // Listen to the click event removeButton.addEventListener("click", function(e) { // Make sure the button click doesn't submit the form: e.preventDefault(); e.stopPropagation(); alert("Are you sure to delete?"); // Remove the file preview. _this.removeFile(file); // If you want to the delete the file on the server as well, // you can do the AJAX request here. }); // Add the button to the file preview element. file.previewElement.appendChild(removeButton); }); this.on("success", function(file, data) { if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) { var d = $.parseJSON(data); var fileitem = "<span class='uploadFile-queue-item' id='" + d.fileKey + "'><a>" + d.name + "</a><img border='0' style='padding:2px;cursor:pointer;' onclick=delAttachment('" + d.delurl + "','" + d.fileKey + "','" + d.name + "') title='刪除' src='plug-in/uploadify/img/uploadify-cancel.png' widht='15' height='15'> </span>"; $("#fileslist").html(fileitem); $("#attachment").val(d.fileKey + "," + d.name + ";"); } this.removeFile(file); }); } }); </script>
java后臺(tái)處理文件上傳的代碼:
@RequestMapping(params = "saveFile", method = RequestMethod.POST) public void saveFile(HttpServletRequest request, HttpServletResponse response, TSDocument document) throws Exception{ Map<String, Object> attributes = new HashMap<String, Object>(); TSTypegroup tsTypegroup=systemService.getTypeGroup("fieltype","文檔分類"); TSType tsType = systemService.getType("files","附件", tsTypegroup); String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID String documentTitle = oConvertUtils.getString(request.getParameter("documentTitle"),"uploadfile");// 文件標(biāo)題 if (StringUtil.isNotEmpty(fileKey)) { document.setId(fileKey); document = systemService.getEntity(TSDocument.class, fileKey); document.setDocumentTitle(documentTitle); } document.setBusinessKey(request.getParameter("businessKey")); document.setSubclassname(MyClassLoader.getPackPath(document)); document.setCreatedate(DateUtils.gettimestamp()); document.setTSType(tsType); UploadFile uploadFile = new UploadFile(request, document); uploadFile.setCusPath("files"); uploadFile.setSwfpath("swfpath"); document = systemService.uploadFile(uploadFile); attributes.put("url", document.getRealpath()); attributes.put("fileKey", document.getId()); if (ResourceUtil.getSessionUserName()!=null) { attributes.put("uploadUser", ResourceUtil.getSessionUserName().getUserName()); }else{ attributes.put("uploadUser", "null"); } attributes.put("time", new SimpleDateFormat("yyyy-MM-dd").format(new Date())); attributes.put("name", document.getAttachmenttitle()+"."+document.getExtend()); attributes.put("downloadurl", "commonController.action?viewFile&fileid="+ document.getId()+"&subclassname="); attributes.put("viewhref", "commonController.action?objfileList&fileKey=" + document.getId()); attributes.put("delurl", "commonController.action?delObjFile&fileKey=" + document.getId()); attributes.put("realPath", document.getRealpath()); if(FileUtils.isPicture(document.getExtend())){ attributes.put("imgUrl", document.getRealpath()); } JSONObject js = new JSONObject(attributes); response.getWriter().write(js.toString()); response.getWriter().flush(); }
注意這里的返回值是直接返回的json對(duì)象,如果采用
@RequestMapping(params = "saveFiles", method = RequestMethod.POST) @ResponseBody
則會(huì)報(bào)錯(cuò):
復(fù)制代碼 代碼如下:
[com.framework.core.common.exception.MyExceptionHandler]org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
上述內(nèi)容就是怎么在Java中利用dropzone.js實(shí)現(xiàn)一個(gè)文件拖拽上傳功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。