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";
}
}
前端頁面源碼
<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";
}
}
前端頁面源碼
<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>
運(yùn)行效果

最后,需要注意的是,文件上傳有默認(rèn)的大小限制
在配置文件中加入,即可消除限制
spring.servlet.multipart.max-file-size=-1 spring.servlet.multipart.max-request-size=-1
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL的實(shí)現(xiàn)方法
這篇文章主要介紹了MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java編程中快速排序算法的實(shí)現(xiàn)及相關(guān)算法優(yōu)化
這篇文章主要介紹了Java編程中快速排序算法的實(shí)現(xiàn)及相關(guān)算法優(yōu)化,快速排序算法的最差時(shí)間復(fù)雜度為(n^2),最優(yōu)時(shí)間復(fù)雜度為(n\log n),存在優(yōu)化的空間,需要的朋友可以參考下2016-05-05
Spring Boot高效數(shù)據(jù)聚合之道深入講解
這篇文章主要給大家介紹了關(guān)于Spring Boot高效數(shù)據(jù)聚合之道的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
自定義SpringBoot的白標(biāo)錯(cuò)誤頁面的操作方法
Spring Boot的白標(biāo)錯(cuò)誤頁面是在應(yīng)用程序出現(xiàn)錯(cuò)誤時(shí)(如404或500 HTTP狀態(tài)碼)自動(dòng)生成的默認(rèn)錯(cuò)誤頁面,下面小編給大家分享如何自定義SpringBoot的白標(biāo)錯(cuò)誤頁面,感興趣的朋友跟隨小編一起看看吧2024-06-06
使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app
這篇文章主要介紹了使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app的方法,包括客戶端的響應(yīng)和XML解析等功能,需要的朋友可以參考下2015-11-11
如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容
這篇文章主要介紹了如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot自定義starter實(shí)現(xiàn)過程圖解
這篇文章主要介紹了springboot自定義starter實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java通過匿名類來實(shí)現(xiàn)回調(diào)函數(shù)實(shí)例總結(jié)
這篇文章主要介紹了Java通過匿名類來實(shí)現(xiàn)回調(diào)函數(shù)的例子,回調(diào)函數(shù)就是一種函數(shù)簽名(若干個(gè)輸入?yún)?shù)、一個(gè)輸出參數(shù))的規(guī)范,java雖不存在函數(shù)聲明,但是java可以用接口來強(qiáng)制規(guī)范。具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08

