java實現(xiàn)文件上傳下載功能
更新時間:2021年08月26日 10:20:10 作者:wh456413
這篇文章主要介紹了java實現(xiàn)文件上傳下載功能,上傳單個或多個文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)文件上傳下載的具體代碼,供大家參考,具體內(nèi)容如下
1.上傳單個文件
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.①單個文件上傳 * @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(); /*上傳文件存儲位置*/ 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"; } }
前端頁面源碼
<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.上傳多個文件
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.②多個文件上傳 */ @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"; } }
前端頁面源碼
<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; } }
前端頁面源碼
<p>下載文件,這里設(shè)置默認(rèn)下載文件為Demo.txt,fileName是下載文件名</p> <a href="/testup/download?fileName=Demo.txt" rel="external nofollow" >download file</a>
運行效果
最后,需要注意的是,文件上傳有默認(rèn)的大小限制
在配置文件中加入,即可消除限制
spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中如何將String轉(zhuǎn)JSONObject
這篇文章主要介紹了Java中如何將String轉(zhuǎn)JSONObject,String類型轉(zhuǎn)JSONObject,下面有兩種方式可以進(jìn)行轉(zhuǎn)換,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Java 最優(yōu)二叉樹的哈夫曼算法的簡單實現(xiàn)
這篇文章主要介紹了Java 最優(yōu)二叉樹的哈夫曼算法的簡單實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10java 實現(xiàn)多個list 合并成一個去掉重復(fù)的案例
這篇文章主要介紹了java 實現(xiàn)多個list 合并成一個去掉重復(fù)的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08