java實(shí)現(xiàn)文件上傳下載功能
本文實(shí)例為大家分享了java實(shí)現(xiàn)文件上傳下載的具體代碼,供大家參考,具體內(nèi)容如下
1.上傳單個(gè)文件
Controller控制層
import java.io.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @RequestMapping("testup") public class UploadController { private static Logger LG= LoggerFactory.getLogger(UploadController.class); /** * 9.①單個(gè)文件上傳 * @param file * @param redirectAttributes * @return */ @RequestMapping(value="/upload",method=RequestMethod.POST,consumes="multipart/form-data") public String uploadFile(@RequestParam MultipartFile file,RedirectAttributes redirectAttributes){ if(file.isEmpty()){ redirectAttributes.addFlashAttribute("message", "Plse select file"); return "redirect:/test/index"; } try { String fileName=file.getOriginalFilename(); /*上傳文件存儲(chǔ)位置*/ String destFileName="D:\\whupload"+File.separator+fileName; File destFile=new File(destFileName); file.transferTo(destFile); //文件上傳成功顯示 //redirectAttributes.addAttribute("message","upload file success."); redirectAttributes.addFlashAttribute("message", "upload file success."); } catch (Exception e) { //文件上傳失敗顯示 redirectAttributes.addFlashAttribute("message", "upload file fail"); LG.debug(e.getMessage()); } return "redirect:/test/index"; } }
前端頁(yè)面源碼
<p>上傳文件,使用multipart/form-data類型</p> <form action="/testup/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <button type="submit">上傳</button> </form>
2.上傳多個(gè)文件
Controller控制層
import java.io.File; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @RequestMapping("testup") public class UploadController { private static Logger LG= LoggerFactory.getLogger(UploadController.class); /** * 9.②多個(gè)文件上傳 */ @RequestMapping(value="/uploadBatchFile",method=RequestMethod.POST,consumes="multipart/form-data") public String uploadBatchFile(@RequestParam MultipartFile[] files,RedirectAttributes redirectAttributes){ boolean isEmpty=true; try { for (MultipartFile multipartFile : files) { if(multipartFile.isEmpty()){ continue; } String fileName=multipartFile.getOriginalFilename(); String destFileName="D:\\whupload"+File.separator+fileName; File destFile=new File(destFileName); multipartFile.transferTo(destFile); isEmpty=false; } //int i=1/0; //localhost:8086/test/index?message=upload file success //redirectAttributes.addAttribute("message","upload file success."); } catch (Exception e) { // TODO Auto-generated catch block redirectAttributes.addFlashAttribute("message", "upload file fail"); LG.debug(e.getMessage()); return "redirect:/test/index"; } if(isEmpty){ redirectAttributes.addFlashAttribute("message", "Plse select file"); }else{ redirectAttributes.addFlashAttribute("message", "upload file success."); } return "redirect:/test/index"; } }
前端頁(yè)面源碼
<form action="/testup/uploadBatchFile" method="post" enctype="multipart/form-data"> <input type="file" name="files"> <input type="file" name="files"> <button type="submit">上傳</button> </form>
3.下載文件
Controller控制器
import java.io.File; import java.net.MalformedURLException; import java.nio.file.Paths; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("testup") public class UploadController { private static Logger LG= LoggerFactory.getLogger(UploadController.class); /** * 10.下載文件 */ @RequestMapping("/download") @ResponseBody public ResponseEntity<Resource> downloadFile(@RequestParam String fileName){ try { Resource resource=new UrlResource( //拼接下載的文件的原路徑 Paths.get("D:/whupload"+File.separator+fileName).toUri()); if(resource.exists() && resource.isReadable()){ return ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream") .header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+ resource.getFilename()+"\"").body(resource); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); LG.debug(e.getMessage()); } return null; } }
前端頁(yè)面源碼
<p>下載文件,這里設(shè)置默認(rèn)下載文件為Demo.txt,fileName是下載文件名</p> <a href="/testup/download?fileName=Demo.txt" rel="external nofollow" >download file</a>
運(yùn)行效果
最后,需要注意的是,文件上傳有默認(rèn)的大小限制
在配置文件中加入,即可消除限制
spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)AWT四大事件的詳細(xì)過(guò)程
AWT的事件處理是一種委派式事件處理方式:普通組件(事件源)將整個(gè)事件處理委托給特定的對(duì)象(事件監(jiān)聽(tīng)器);當(dāng)該事件源發(fā)生指定的事件時(shí),就通知所委托的事件監(jiān)聽(tīng)器,由事件監(jiān)聽(tīng)器來(lái)處理這個(gè)事件2022-04-04Java計(jì)算兩個(gè)日期時(shí)間之間的天數(shù)最簡(jiǎn)方法
這篇文章給大家分享了Java計(jì)算兩個(gè)日期時(shí)間之間的天數(shù)最簡(jiǎn)單的實(shí)現(xiàn)方法,有興趣的朋友可以參考學(xué)習(xí)下。2018-07-07Java中如何將String轉(zhuǎn)JSONObject
這篇文章主要介紹了Java中如何將String轉(zhuǎn)JSONObject,String類型轉(zhuǎn)JSONObject,下面有兩種方式可以進(jìn)行轉(zhuǎn)換,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Java 最優(yōu)二叉樹(shù)的哈夫曼算法的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Java 最優(yōu)二叉樹(shù)的哈夫曼算法的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10java 實(shí)現(xiàn)多個(gè)list 合并成一個(gè)去掉重復(fù)的案例
這篇文章主要介紹了java 實(shí)現(xiàn)多個(gè)list 合并成一個(gè)去掉重復(fù)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08