springboot 中文件上傳下載實例代碼
Spring Boot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進行配置,從而使開發(fā)人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發(fā)展的快速應(yīng)用開發(fā)領(lǐng)域(rapid application development)成為領(lǐng)導(dǎo)者。
Spring Boot特點
1. 創(chuàng)建獨立的Spring應(yīng)用程序
2. 嵌入的Tomcat,無需部署WAR文件
3. 簡化Maven配置
4. 自動配置Spring
5. 提供生產(chǎn)就緒型功能,如指標,健康檢查和外部配置
6. 絕對沒有代碼生成和對XML沒有要求配置[
springboot 實現(xiàn)文件上傳下載實例代碼如下所示:
@Controller
public class FileUploadCtrl {
@Value("${file.upload.dir}")
private String path;
/**
* 實現(xiàn)文件上傳
* */
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> fileUpload(@RequestParam("fileName") MultipartFile file){
Map<String,Object> map = new HashMap<String, Object>();
int no = 0;
String msg = "上傳失敗!";
if(!file.isEmpty()){
String fileName = file.getOriginalFilename();
File dest = new File(path + "/" + fileName);
if(!dest.getParentFile().exists()){ //判斷文件父目錄是否存在
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest); //保存文件
no = 1;
msg = "上傳成功!";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
map.put("no",no);
map.put("msg", msg);
return map;
}
@RequestMapping(
value = "/fileDownload",
method = RequestMethod.GET
)
public ResponseEntity<?> getGwFileContent(@RequestParam String fileName,@RequestParam int flag) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
String filepath = path+"/"+fileName;;
InputStream is = null;
try {
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", new String(fileName.getBytes("GBK"), "ISO8859-1")));
if(flag==0){//表示獲取縮略圖
File file = new File(filepath);
filepath = path+"/xx"+fileName;
File xxFile = new File(filepath);
if(!xxFile.exists()){//不存在就生成縮略圖
Thumbnails.of(file).scale(0.25f).toFile(xxFile);
}
}
is = new FileInputStream(new File(filepath));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(is));
}
}
總結(jié)
以上所述是小編給大家介紹的springboot 中文件上傳下載實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- SpringBoot中的文件上傳與下載詳解
- Java實現(xiàn)大文件的分片上傳與下載(springboot+vue3)
- SpringBoot文件上傳與下載功能實現(xiàn)詳解
- Axios+Spring?Boot實現(xiàn)文件上傳和下載
- Spring?Boot實現(xiàn)文件上傳下載
- SpringBoot上傳和下載文件的原理解析
- SpringBoot 文件或圖片上傳與下載功能的實現(xiàn)
- springboot+vue實現(xiàn)文件上傳下載
- 詳解SpringBoot下文件上傳與下載的實現(xiàn)
- Spring Boot 文件上傳與下載的示例代碼
- SpringBoot 文件上傳和下載的實現(xiàn)源碼
- SpringBoot實現(xiàn)文件上傳下載功能小結(jié)
- SpringBoot+ruoyi框架文件上傳和下載的實現(xiàn)
相關(guān)文章
詳解在SpringBoot中使用MongoDb做單元測試的代碼
這篇文章主要介紹了詳解在SpringBoot中使用MongoDb做單元測試的代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot+MybatisPlus實現(xiàn)sharding-jdbc分庫分表的示例代碼
本文主要介紹了SpringBoot+MybatisPlus實現(xiàn)sharding-jdbc分庫分表的示例代碼,以分庫,分表,分庫分表三種方式來實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03
Java基于裝飾者模式實現(xiàn)的圖片工具類實例【附demo源碼下載】
這篇文章主要介紹了Java基于裝飾者模式實現(xiàn)的圖片工具類,結(jié)合完整實例形式分析了裝飾者模式實現(xiàn)圖片的判斷、水印、縮放、復(fù)制等功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09
Java面試官最喜歡問的關(guān)鍵字之volatile詳解
這篇文章主要給大家介紹了關(guān)于Java面試官最喜歡問的關(guān)鍵字之volatile的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
使用JavaWeb webSocket實現(xiàn)簡易的點對點聊天功能實例代碼
這篇文章主要介紹了使用JavaWeb webSocket實現(xiàn)簡易的點對點聊天功能實例代碼的相關(guān)資料,內(nèi)容介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
Java多線程中線程池常見7個參數(shù)的詳解以及執(zhí)行流程
本文主要介紹了Java多線程中線程池常見7個參數(shù)的詳解以及執(zhí)行流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

