本篇文章為大家展示了SpringMVC中怎么單文件和多文件上傳功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),蕪湖企業(yè)網(wǎng)站建設(shè),蕪湖品牌網(wǎng)站建設(shè),網(wǎng)站定制,蕪湖網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,蕪湖網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。SpringMVC中寫好了文件上傳的類。
要使用文件上傳,首先需要文件上傳相關(guān)的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()來復(fù)制文件,因此使用流來輸出到磁盤
單文件多文件只是將單文件中傳遞來的file參數(shù)改為數(shù)組形式,將方法內(nèi)的file有關(guān)的操作都變?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"; }
上述內(nèi)容就是SpringMVC中怎么單文件和多文件上傳功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:SpringMVC中怎么單文件和多文件上傳功能-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://chinadenli.net/article20/cehdco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、面包屑導(dǎo)航、網(wǎng)站維護、定制開發(fā)、建站公司、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容