SpringMVC 單文件,多文件上傳實現(xiàn)詳解
需要用到的流的相關知識:http://chabaoo.cn/article/170640.htm
SpringMVC中寫好了文件上傳的類。
要使用文件上傳,首先需要文件上傳相關的Jar包。commons-fileupload.jar 和 commons-io.jar。
添加到pom.xml或lib文件夾下。
pom.xml:
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
在SprigMVC的配置文件中添加bean(id和class都是固定寫法):
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="104857600"></property> </bean>
前端寫一個單文件上傳的表單,一個多文件上傳的表單(多文件上傳的表單中,多個文件輸入input中的name相同):
<form action="handler/testUpload" method="post" enctype="multipart/form-data"> 文件描述: <input type="text" name="desc" id="desc"> <br> 請選擇文件: <input type="file" name="file"><br> <input type="submit" value="上傳文件"> </form> <br> <br> <form action="handler/testMutiUpload" method="post" enctype="multipart/form-data"> 文件描述: <input type="text" name="desc"> <br> 請選擇文件: <input type="file" name="file"><br> 請選擇文件1: <input type="file" name="file"><br> 請選擇文件2: <input type="file" name="file"><br> <input type="submit" value="上傳文件"> </form>
文件上傳中,參數(shù)要使用MultipartFile而不是File類,不能使用FileUtils.copyFile()來復制文件,因此使用流來輸出到磁盤
單文件多文件只是將單文件中傳遞來的file參數(shù)改為數(shù)組形式,將方法內的file有關的操作都變?yōu)閿?shù)組即可。
單文件上傳
也可以不使用流,下面這句看到有人使用,但是沒有測試。
File dest = new File(filePath + fileName); file.transferTo(dest);
@RequestMapping("testUpload") public String testUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException { System.out.println("文件描述:" + desc); // 得到文件的輸入流 InputStream inputStream = file.getInputStream(); // 得到文件的完整名字 img.png/hh.docx String fileName = file.getOriginalFilename(); // 輸出流 OutputStream outputStream = new FileOutputStream("C:\\tmp\\" + fileName); // 緩沖區(qū) byte[] bs = new byte[1024]; int len = -1; while ((len = inputStream.read(bs)) != -1) { outputStream.write(bs,0,len); } inputStream.close(); outputStream.close(); return "success"; }
多文件上傳
@RequestMapping("testMutiUpload") public String testMutiUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile[] files) throws IOException { System.out.println("文件描述:" + desc); for (MultipartFile file : files) { InputStream inputStream = file.getInputStream(); String fileName = file.getOriginalFilename(); OutputStream outputStream = new FileOutputStream("C:\\tmp\\" + fileName); byte[] bs = new byte[1024]; int len = -1; while ((len = inputStream.read(bs)) != -1) { outputStream.write(bs,0,len); } inputStream.close(); outputStream.close(); } return "success"; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Tomcat啟動分析(我們?yōu)槭裁匆渲肅ATALINA_HOME環(huán)境變量)
本文主要介紹Tomcat啟動分析的知識,這里整理了相關資料及分析原因和如何實現(xiàn)的方法,有興趣的小伙伴可以參考下2016-09-09java 內部類(匿名類,匿名對象,靜態(tài)內部類)詳解及實例
這篇文章主要介紹了java 內部類詳解及實例代碼的相關資料,需要的朋友可以參考下2016-12-12springboot接收日期字符串參數(shù)與返回日期字符串類型格式化
這篇文章主要介紹了springboot接收日期字符串參數(shù)與返回日期字符串類型格式化,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01