Spring MVC實(shí)現(xiàn)文件上傳和下載
本文實(shí)例為大家分享了Spring MVC實(shí)現(xiàn)文件上傳和下載的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳
1、導(dǎo)入主要依賴
<!--文件上傳--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <!--servlet-api導(dǎo)入高版本的--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency>
2、配置bean:multipartResolver(注:此bena的id必須為:multipartResolver , 否則上傳文件會(huì)報(bào)400的錯(cuò)誤?。?/p>
<!--文件上傳配置--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 請(qǐng)求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內(nèi)容, 默認(rèn)為ISO-8859-1 --> <property name="defaultEncoding" value="utf-8"/> <!-- 上傳文件大小上限,單位為字節(jié)(10485760=10M) --> <property name="maxUploadSize" value="10485760"/> <property name="maxInMemorySize" value="40960"/> </bean>
3、index.jsp
4、Controller
@RestController public class FileController { //@RequestParam("file") 將name=file控件得到的文件封裝成 CommonsMultipartFile 對(duì)象 //批量上傳 CommonsMultipartFile 則為數(shù)組即可 @RequestMapping("/upload") public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { //獲取文件名 : file.getOriginalFilename(); String uploadFileName = file.getOriginalFilename(); //如果文件名為空,直接回到首頁(yè)! if ("".equals(uploadFileName)) { return "redirect:/index.jsp"; } System.out.println("上傳文件名 : " + uploadFileName); //上傳路徑保存設(shè)置 String path = request.getServletContext().getRealPath("/upload"); //如果路徑不存在,創(chuàng)建一個(gè) File realPath = new File(path); if (!realPath.exists()) { realPath.mkdir(); } System.out.println("上傳文件保存地址:" + realPath); InputStream is = file.getInputStream(); //文件輸入流 OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件輸出流 //讀取寫(xiě)出 int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); os.flush(); } os.close(); is.close(); return "redirect:/index.jsp"; } /* * 采用file.Transto 來(lái)保存上傳的文件 */ @RequestMapping("/upload2") public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { //上傳路徑保存設(shè)置 String path = request.getServletContext().getRealPath("/upload"); File realPath = new File(path); if (!realPath.exists()) { realPath.mkdir(); } System.out.println("上傳文件保存地址:" + realPath); //通過(guò)CommonsMultipartFile的方法直接寫(xiě)文件(注意這個(gè)時(shí)候) file.transferTo(new File(realPath + "/" + file.getOriginalFilename())); return "redirect:/index.jsp"; } }
文件下載
index.jsp
Controller
@RequestMapping(value = "/download") public String downloads(HttpServletResponse response, HttpServletRequest request) throws Exception { //要下載的圖片地址 String path = request.getServletContext().getRealPath("/upload"); String fileName = "基礎(chǔ)語(yǔ)法.jpg"; //1、設(shè)置response 響應(yīng)頭 response.reset(); //設(shè)置頁(yè)面不緩存,清空buffer response.setCharacterEncoding("UTF-8"); //字符編碼 response.setContentType("multipart/form-data"); //二進(jìn)制傳輸數(shù)據(jù) //設(shè)置響應(yīng)頭 response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8")); File file = new File(path, fileName); //2、 讀取文件--輸入流 InputStream input = new FileInputStream(file); //3、 寫(xiě)出文件--輸出流 OutputStream out = response.getOutputStream(); byte[] buff = new byte[1024]; int index = 0; //4、執(zhí)行 寫(xiě)出操作 while ((index = input.read(buff)) != -1) { out.write(buff, 0, index); out.flush(); } out.close(); input.close(); return null; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
idea導(dǎo)入工程時(shí)不能導(dǎo)入maven項(xiàng)目不能加入tomcatServer的原因
這篇文章主要介紹了idea導(dǎo)入工程時(shí)不能導(dǎo)入maven項(xiàng)目不能加入tomcatServer的原因及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Mybatis-Plus處理Mysql?Json類型字段的詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于Mybatis-Plus處理Mysql?Json類型字段的詳細(xì)教程,Mybatis-Plus可以很方便地處理JSON字段,在實(shí)體類中可以使用@JSONField注解來(lái)標(biāo)記JSON字段,同時(shí)在mapper.xml中使用json函數(shù)來(lái)操作JSON字段,需要的朋友可以參考下2024-01-01SpringBoot項(xiàng)目中org.junit.jupiter.api.Test報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了SpringBoot項(xiàng)目中org.junit.jupiter.api.Test報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Java?LinkedList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?LinkedList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Eclipse中使用Maven創(chuàng)建Java Web工程的實(shí)現(xiàn)方式
這篇文章主要介紹了Eclipse中使用Maven創(chuàng)建Java Web工程的實(shí)現(xiàn)方式的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的方式,需要的朋友可以參考下2017-10-10