spring mvc實(shí)現(xiàn)文件上傳與下載功能
本文實(shí)例為大家分享了spring mvc實(shí)現(xiàn)文件上傳與下載功能的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳
在pom.xml中引入spring mvc以及commons-fileupload的相關(guān)jar
<!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.13.RELEASE</version> </dependency> <!-- 文件上傳與下載 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
在springmvc.xml中加入文件上傳的相關(guān)配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 上傳文件大小上限,單位為字節(jié)(10MB) --> <property name="maxUploadSize"> <value>10485760</value> </property> <!-- 請(qǐng)求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內(nèi)容,默認(rèn)為ISO-8859-1 --> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean>
在jsp文件中加入form表單
<form action="upload" enctype="multipart/form-data" method="post"> <table> <tr> <td>文件描述:</td> <td><input type="text" name="description"></td> </tr> <tr> <td>請(qǐng)選擇文件:</td> <td><input type="file" name="file"></td> </tr> <tr> <td><input type="submit" value="上傳"></td> </tr> </table> </form>
添加文件上傳的方法
//上傳文件會(huì)自動(dòng)綁定到MultipartFile中 @RequestMapping(value="/upload",method=RequestMethod.POST) public String upload(HttpServletRequest request, @RequestParam("description") String description, @RequestParam("file") MultipartFile file) throws Exception { //如果文件不為空,寫入上傳路徑 if(!file.isEmpty()) { //上傳文件路徑 String path = request.getServletContext().getRealPath("/file/"); //上傳文件名 String filename = file.getOriginalFilename(); File filepath = new File(path,filename); //判斷路徑是否存在,如果不存在就創(chuàng)建一個(gè) if (!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } //將上傳文件保存到一個(gè)目標(biāo)文件當(dāng)中 file.transferTo(new File(path + File.separator + filename)); return "success"; } else { return "error"; } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Sharding-Proxy分庫分表和數(shù)據(jù)加密使用場景分析
這篇文章主要介紹了Sharding-Proxy分庫分表和數(shù)據(jù)加密使用經(jīng)驗(yàn)分享,通過場景模擬分析結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Java中的synchronized?優(yōu)化方法之鎖膨脹機(jī)制
這篇文章主要介紹了Java中的synchronized?優(yōu)化方法之鎖膨脹機(jī)制,鎖膨脹機(jī)制是提升?synchronized?性能最有利的方法之一,下面我們就來看看什么事鎖膨脹及鎖膨脹的各種細(xì)節(jié)2022-05-05SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn)
這篇文章主要為大家介紹了SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實(shí)踐
hsqldb只需要添加對(duì)應(yīng)的依賴,然后在配置文件進(jìn)行配置。不需要安裝一個(gè)數(shù)據(jù)庫,本文就來介紹一下具體使用,感興趣的可以了解一下2021-09-09httpclient 請(qǐng)求http數(shù)據(jù),json轉(zhuǎn)map的實(shí)例
下面小編就為大家?guī)硪黄猦ttpclient 請(qǐng)求http數(shù)據(jù),json轉(zhuǎn)map的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Java基于外觀模式實(shí)現(xiàn)美食天下食譜功能實(shí)例詳解
這篇文章主要介紹了Java基于外觀模式實(shí)現(xiàn)美食天下食譜功能,較為詳細(xì)的講述了外觀模式的概念、原理并結(jié)合實(shí)例形似詳細(xì)分析了Java基于外觀模式實(shí)現(xiàn)美食天下食譜功能的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-05-05springboot整合mybatis-plus逆向工程的實(shí)現(xiàn)
這篇文章主要介紹了springboot整合mybatis-plus逆向工程的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08