利用Spring處理檔案上傳,spring處理檔案上傳

來源:互聯網
上載者:User

利用Spring處理檔案上傳,spring處理檔案上傳

檔案上傳有很多中方式,之前寫過一個利用DiskFileItemFactory實現檔案上傳的例子,這次項目中使用了新的方式,通過MultipartHttpServletRequest來擷取檔案流,下面是我的一個DEMO:

輸出方式一:
<span style="white-space:pre"></span>//對request進行封裝MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;//根據名稱擷取MultipartFile檔案對象MultipartFile headFile = multipartRequest.getFile("headFile");logger.debug("--------------------------------------------->file operation starting...");logger.debug("----------------->headFile:"+headFile.getName()+"\ttype:"+headFile.getContentType()+"\tsize:"+headFile.getSize());//擷取檔案流FileInputStream headIn = (FileInputStream) headFile.getInputStream();FileOutputStream headOut = new FileOutputStream("d:/headFile.jpeg");byte[] bytes = new byte[1024];do{headIn.read(bytes,0,1024);headOut.write(bytes);}while(headIn.available() > 0);headIn.close();headOut.close();logger.debug("--------------------------------------------->file operation success!!!");

檔案上傳到了指定目錄下了!使用過的朋友可能會問到了,為什麼不用另一種更簡單的方式呢?別急,下面我們看MultipartFile內建的輸出方法。輸出方式二:
<span style="white-space:pre"></span>//對request進行封裝MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;//根據名稱擷取MultipartFile檔案對象MultipartFile headFile = multipartRequest.getFile("headFile");logger.debug("--------------------------------------------->file operation starting...");logger.debug("----------------->headFile:"+headFile.getName()+"\ttype:"+headFile.getContentType()+"\tsize:"+headFile.getSize());//直接輸出到執行的目錄下headFile.transferTo(new File("d:/little bear.jpeg"));

看到了吧,同樣的,檔案上傳了╮(╯▽╰)╭很簡單吧!下面我們看看MultipartFile這個介面源碼:
interface MultipartFile {<span style="white-space:pre"></span>public byte[] getBytes();<span style="white-space:pre"></span>public String getContentType();<span style="white-space:pre"></span>public java.io.InputStream getInputStream();<span style="white-space:pre"></span>public String getName();<span style="white-space:pre"></span>public String getOriginalFilename();<span style="white-space:pre"></span>public long getSize();<span style="white-space:pre"></span>public boolean isEmpty();<span style="white-space:pre"></span>public void transferTo(java.io.File dest);}

在MultipartFile介面中定義了如下很多有用的方法。

l、使用getSize()方法獲得檔案長度,以此決定允許上傳的檔案大小。

2、使用isEmpty()方法判斷上傳檔案是否為空白檔案,以此決定是否拒絕空檔案。

3、使用getInputStream()方法將檔案讀取為java.io.InputStream流對象。

4、使用getContentType()方法獲得檔案類型,以此決定允許上傳的檔案類型。

5、使用transferTo(dest)方法將上傳檔案寫到伺服器上指定的檔案。

利用這些方法組合我們可以對上傳的檔案進行限制,舉一反三不講解了,僅此拋個磚╮(╯▽╰)╭
作者:佇望碧落 出處:http://blog.csdn.net/cl05300629


Spring MVC 上傳檔案大小怎處理

spring設定檔可以設定上傳檔案的大小。
 
spring檔案上傳不同瀏覽器不可以顯示成功資訊

還請貼JAVA代碼。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.