上傳下載組件SmartUpload使用方法

來源:互聯網
上載者:User

一、SmartUpload組件簡介

 

SmartUpload是由www.jspsmart.com網站開發的一個可免費使用的全功能的檔案上傳下載組件,適於嵌入執行上傳下載操作的jsp檔案中。該組件有以下幾個特點: 

1、使用簡單。在jsp檔案中僅僅書寫三五行java代碼就可以搞定檔案的上傳或下載,方便。 

2、能全程式控制制上傳。利用SmartUpload組件提供的對象及其操作方法,可以獲得全部上傳檔案的資訊(包括檔案名稱,大小,類型,副檔名,檔案資料等),方便存取。 

3、能對上傳的檔案在大小、類型等方面做出限制。如此可以濾掉不符合要求的檔案。 

4、下載靈活。僅寫兩行代碼,就能把web伺服器變成檔案伺服器。不管檔案在web伺服器的目錄下或在其它任何目錄下,都可以利用SmartUpload進行下載。 

5、能將檔案上傳到資料庫中,也能將資料庫中的資料下載下來。這種功能針對的是mysql資料庫,因為不具有通用性,所以本文不準備舉例介紹這種用法。

本篇將講述java常用的上傳/下載組件 SmartUpload 的詳細java的朋友不會陌生,幾乎在所有的B/S架構的項目或者產品中都會用到檔案的上傳/下載。

 

 

二、SmartUpload組件使用

(1)檔案上傳

HTML代碼

<%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.httpupload4j.SmartUpload"%><%@ page import="com.soft4j.bo.WebFileMgr"%><%<br />String ret = null;<br />SmartUpload su = null;<br />try{<br />su = new SmartUpload();<br />su.initialize(pageContext);<br />su.upload();<br />ret = WebFileMgr.upload(su,pageContext);//WebFileMgr.java 處理檔案上傳<br />if(ret!=null){<br />out.print("successed");<br />}<br />}catch(Exception e){<br />out.print("faild");<br />}finally{<br />su = null;<br />}<br />%>

WebFileMgr.upload(su,pageContext)方法內容:

Java代碼

/**<br /> * 上傳方法.<br /> * @param su<br /> * @param pageContext<br /> * @return<br /> * @throws Exception<br /> */<br /> public static String upload(SmartUpload su,PageContext pageContext) throws Exception {<br /> com.soft4j.httpupload4j.File suFile = null;<br /> int fileCount = 0;<br /> int maxFileSize = 900;//單個檔案最大為900K<br /> String AllowedExtensions=",jpg,jpeg,gif,png,";//允許上傳的檔案類型<br /> try {</p><p> for (int i=0; i<su.getFiles().getCount();i++) {<br /> suFile = su.getFiles().getFile(i);<br /> if (suFile.isMissing())<br /> continue;<br /> fileSize = suFile.getSize()/1024;//位元組轉換成KB<br /> if(fileSize==0) fileSize=1;</p><p> if(maxFileSize<fileSize) throw new Exception("單個上傳檔案的容量不能超過["+maxFileSize+"KB]");</p><p> if (suFile.getFileExt() == null<br /> || "".equals(suFile.getFileExt())) {<br /> fileExt = ",,";<br /> } else {<br /> fileExt = "," + suFile.getFileExt().toLowerCase() + ",";<br /> }</p><p> if (!"".equals(AllowedExtensions)<br /> && AllowedExtensions.indexOf(fileExt) == -1) {<br /> throw new Exception("您上傳的檔案[" + suFile.getFileName()<br /> + "]的類型為系統禁止上傳的檔案類型,不能上傳!");<br /> }</p><p> fileCount++;<br /> }<br /> if (fileCount==0) throw new Exception("請選擇上傳的檔案");</p><p> StringBuffer fullFileName = null;//儲存到伺服器上的檔案名稱(帶路徑)<br /> for (int i=0; i<su.getFiles().getCount();i++) {<br /> suFile = su.getFiles().getFile(i);<br /> if (suFile.isMissing()) continue;<br /> fullFileName = new StringBuffer("檔案的路徑");//填寫 檔案的路徑<br /> fullFileName.append("檔案名稱");//填寫 檔案名稱<br /> suFile.saveAs(fullFileName.toString(),SmartUpload.SAVE_PHYSICAL);<br /> }<br /> return "successed";<br /> } finally {<br /> //<br /> }<br /> }

上面的JSP,Java代碼就可以實現檔案的上傳.

(2)檔案下載

下載相對更為簡單,只要設定下載的檔案名稱(帶路徑)就可以了.

Html代碼

<%@ page contentType="text/html;charset=UTF-8"%><br /><%@ page import="com.soft4j.httpupload4j.SmartUpload"%><br /><%<br />//建立一個SmartUpload對象<br /> SmartUpload su = new SmartUpload();<br /> //初始化<br /> su.initialize(pageContext);<br /> //設定要下載的檔案名稱(帶路徑)<br /> String fileName="下載的檔案名稱";//檔案名稱(帶路徑)<br /> try{<br /> su.downloadFile(fileName);//下載檔案<br /> }catch(Exception e){<br /> e.printStackTrace();<br /> out.println(e.toString());<br /> }<br /> response.getOutputStream().close();<br />%>  

 

三、其他學習資料

1、jspSmartUpload組件中的主要類:

http://www.cnblogs.com/jingua1026/articles/1321642.html

2、下載介紹

http://www.javaeye.com/wiki/topic/157247

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.