swfupload文件上傳與下載.doc_第1頁
swfupload文件上傳與下載.doc_第2頁
swfupload文件上傳與下載.doc_第3頁
swfupload文件上傳與下載.doc_第4頁
swfupload文件上傳與下載.doc_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

文件上傳與下載一、 文件上傳與下載所需要文件(*.js,*.css,*.swf)1.存放位置總共7個(gè)相關(guān)文件;2.引入包的位置跟平時(shí)引入js一樣,這兒需要引入5個(gè)相關(guān)文件。link href=/swfupload/default.css rel=stylesheet type=text/css /script type=text/javascript src=/swfupload/swfupload.jsscript type=text/javascript src=/swfupload/swfupload.queue.jsscript type=text/javascript src=/swfupload/fileprogress.jsscript type=text/javascript src=/swfupload/handlers.js二、 設(shè)置需要上傳的數(shù)據(jù)庫字段(jsp頁面)1. 比如在內(nèi)容管理下,有基礎(chǔ)學(xué)習(xí)模塊,其中有一個(gè)字段是accessory現(xiàn)在進(jìn)行一下設(shè)置:a. name=webContent.accessory,這是你要上傳數(shù)據(jù)庫字段。b. readonly=readonly是為了設(shè)置文件上傳成功后,該文本框?qū)⒉荒鼙恍薷?。c.校驗(yàn)數(shù)據(jù)庫字段如果該字段可以為空,校驗(yàn)如下,當(dāng)然要在國際化資源文件和相應(yīng)的ActionBean中配置相關(guān)信息。 如果不為空,再加上一句*三、在JSP設(shè)置swf,即上傳功能/校驗(yàn) 文件上傳 大小、格式 、進(jìn)度條var swfu = new SWFUpload( upload_url : /upload.action?moduleName=ccm,file_size_limit : 5 MB,file_types : *.doc;*.docx;*.xlx;*.txt;*.xlsx;,file_types_description : *.doc;*.docx;*.xlx;*.txt;*.xlsx;,file_upload_limit : 1,custom_settings : uploadDom : webContent.accessory,button_placeholder_id : spanButtonPlaceHolder);/刪除文件function deleteFile(filePath, fileProgressID) jQuery.ajax( type : post,url : /upload.action,data : deleteFile=&Filename= + filePath,success : function(result) if (result != ) jQuery(# + fileProgressID).toggle(1000);jQuery(#up).val();jQuery(#divStatus).remove();var stats = swfu.getStats();stats.successful_uploads-;swfu.setStats(stats););1.文件上傳路徑upload_url : /upload.action?moduleName=ccm,這樣他會(huì)去找相應(yīng)的ActionBean。2.文件上傳大小file_size_limit : 5 MB,這需要你的情況自己設(shè)定,最大不能超過500M。3.文件上傳的類型file_types : *.doc;*.docx;*.xlx;*.txt;*.xlsx;,當(dāng)然也可以上傳圖片。這根據(jù)情況而定。4.上傳文件的個(gè)數(shù)file_upload_limit : 1,目前只能上傳單個(gè)文件。5.設(shè)置數(shù)據(jù)庫字段uploadDom : webContent.accessory,文件上傳成功后就將保存路徑保存在數(shù)據(jù)庫中。三、 自己對(duì)于的ActionBean中處理1. 修改上傳文件時(shí),先要?jiǎng)h除原有的文件在saveOrupdate方法中/* 修改 */if (StringUtils.isNotEmpty(baseContent.getPreAccessory() if (!(baseContent.getPreAccessory().equals(baseContent.getAccessory() File file = new File(context.getServletContext().getRealPath(baseContent.getPreAccessory();/ 刪掉原有的附件file.delete();2. 當(dāng)刪除單條數(shù)據(jù)后,也刪除相應(yīng)的附件3. /*4. * 單條刪除成功之后將相應(yīng)的附件刪除5. * 6. * throws DaoException7. * throws ServiceException8. * throws Exception9. */10. public void afterDelete() throws DaoException, ServiceException, Exception 11. if (null != baseContent) 12. if (StringUtils.isNotEmpty(baseContent.getAccessory() 13. File file = new File(context.getServletContext().getRealPath(baseContent.getAccessory();14. file.delete();15. 16. 17. 3.當(dāng)批量刪除數(shù)據(jù)后,也要批量刪除相應(yīng)的附件2. /*3. * 批量刪除成功之后將相應(yīng)的附件刪除4. * 5. * throws DaoException6. * throws ServiceException7. * throws Exception8. */9. public void afterDeleteAll() throws DaoException, ServiceException, Exception 10. / 刪除所有視頻附件11. if (ListUtils.isNotEmpty(assessoryNames) 12. for (String str : assessoryNames) 13. File file = new File(context.getServletContext().getRealPath(str);14. file.delete();15. 16. 17. 四、 公用的ActionBean(UploadActionBean)的設(shè)置在這里面,大家可以不用設(shè)置任何數(shù)據(jù),只是作為了解。1. 上傳文件的方法2. /*3. * 上傳文件4. * 5. * return6. */7. DefaultHandler8. public Resolution upload() 9. if (filedata != null) 10. try 11. String realPath = getSavePath() + / + moduleName;12. File directory = new File(realPath);13. / 創(chuàng)建目錄14. if (!directory.exists() 15. directory.mkdir();16. 17. / 附件上傳成功18. filedata.save(new File(realPath + / + filedata.getFileName();19. / 得到文件內(nèi)容的hash碼20. hashName = this.getFileMD5(realPath + / + filedata.getFileName();21. File file = new File(realPath + / + filedata.getFileName();22. String saveName = hashName + filedata.getFileName();23. / 將沒有轉(zhuǎn)碼的文件刪除掉24. File fileNew = new File(realPath + / + saveName);25. if (fileNew.exists() 26. file.delete();27. 28. if (file.exists() 29. file.renameTo(new File(realPath + / + saveName);30. 31. / 返回附件保存路徑32. this.getContext().getRequest().setAttribute(filePath, /upload/ + moduleName + / + saveName);33. return new ForwardResolution(swfupload/success.jsp);34. catch (IOException e) 35. logger.error(Error while writing file : + filedata.getFileName();36. 37. 38. return null;39. 40.2.刪除文件的方法/* * 刪除文件 * */public Resolution deleteFile() try if (StringUtils.isNotEmpty(filename) String realPath = getSavePath(filename);File file = new File(realPath);if (file.exists() if (file.delete() return new StreamingResolution(AjaxType.SENDUTF8, Y);return new StreamingResolution(AjaxType.SENDUTF8, ); catch (Exception e) return new StreamingResolution(AjaxType.SENDUTF8, getStripesError(ajax.error);五、 文件的下載1. 在相應(yīng)的下載頁面(show.jsp)$actionBean.webContent.accessory2.其中需要將數(shù)據(jù)庫保存的路徑傳到UploadActionBean中3.截取文件名稱,用于在頁面顯示對(duì)應(yīng)的JS/用于截取附件名稱,使上傳文件名稱和下載一樣jQuery.fn.limit=function() var self = $(divlimit); self.each(function() var objString = $(this).text(); var objLength = $(this).text().length; var num = $(this).attr(limit); if(objLength num) $(this).attr(title,objString); objString = $(this).text(objString.substring(num); ) $(function() $(document.body).limit(); ) 4.UploadactionBean中相應(yīng)的方法/* * 下載文件前,得到文件的名稱及相應(yīng)的路徑 */public InputStream buildDownFile() try if(StringUtils.isNotEmpty(this.getAccessory()/ 找到根路徑String filePath = getContext().getServletContext().getRealPath()+this.getAccessory();return new FileInputStream(filePath); catch (FileNotFoundException e) getContext().getValidationErrors().addGlobalError(new SimpleError(沒有找到相應(yīng)的文件);return null;/* * 文件下載 * * throws UnsupportedEncodingException */public Resolution download() throws UnsupportedEncodingException return new StreamingResolution(application/octet-stream, buildDownFile().setFilename(this.getStr(accessory.substring(accessory.lastIndexOf(/)+33);六、 相關(guān)界面原型1. 未

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論