SpringBoot上傳下載文件+oss實(shí)例
更新時(shí)間:2024年04月19日 15:52:44 作者:偷代碼的貓
這篇文章主要介紹了SpringBoot上傳下載文件+oss實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
SpringBoot上傳下載文件+oss
上傳文件
Controller
@ApiOperation(value = "上傳文件", tags = {"通用接口",})
@ApiResponses(value = {@ApiResponse(code = 200, message = "上傳文件", response = ResultVO.class)})
@PostMapping("/upload/file")
public ResultVO uploadFile(@ApiParam(value = "文件") @RequestParam("file") MultipartFile file,
@ApiParam(value = "id") @RequestParam("id")Integer id) {
String filePath = "xx/"+file.getOriginalFilename();
ResultVO resultVO = new ResultVO();
resultVO.setCode(200);
resultVO.setMessage("上傳成功");
try {
ossUtil.uploadFile(file.getInputStream(),filePath);
}catch (Exception e){
resultVO.setMessage("上傳失敗");
e.printStackTrace();
}
return resultVO;
}Service
/**
* 上傳文件
* @param inputStream
* @param fileName
*/
public void uploadFile(InputStream inputStream, String fileName){
OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
String objectName = fileDir+"/"+fileName;
if(!ossClient.doesBucketExist(bucketName)){
ossClient.createBucket(bucketName);
}
ossClient.putObject(bucketName,objectName,inputStream);
ossClient.shutdown();
}下載文件
Controller
/**
* 上傳文件
*/
@ApiOperation(value = "下載文件", tags = {"通用接口",})
@ApiResponses(value = {@ApiResponse(code = 200, message = "下載文件", response = ResultVO.class)})
@GetMapping("/down/file")
public ResponseEntity downFile(@ApiParam(value = "文件名,包括后綴") @RequestParam("name") String name) {
ResultVO resultVO = new ResultVO();
String filePath = "xx/"+name;
resultVO.setCode(200);
resultVO.setMessage("下載成功");
try {
// ossUtil.downloadFile(name,saveDir,filePath);
InputStream inputStream = ossUtil.downloadFile(filePath);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + new String(name.getBytes("UTF-8"),"iso-8859-1"));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return new ResponseEntity<byte[]>(ossUtil.getBytes(inputStream), headers, HttpStatus.OK);
}catch (Exception e){
resultVO.setMessage("下載失敗");
e.printStackTrace();
}
return ResponseEntity.notFound().build();
}Service
/**
* 下載文件
*/
public InputStream downloadFile(String filePath) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
String objectName = fileDir+"/"+filePath;
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
// ossClient.shutdown();
return ossObject.getObjectContent();
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能
- SpringBoot實(shí)現(xiàn)文件下載的限速功能
- SpringBoot中實(shí)現(xiàn)文件上傳、下載、刪除功能的步驟
- SpringBoot實(shí)現(xiàn)文件下載的四種方式
- Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載的使用示例
- SpringBoot+MinIO實(shí)現(xiàn)文件上傳、讀取、下載、刪除的使用示例
- SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)
- SpringBoot返回文件使前端下載的幾種方式小結(jié)
相關(guān)文章
Java出現(xiàn)中文亂碼問(wèn)題分析及解決方案
在Java開(kāi)發(fā)中,處理中文亂碼是一個(gè)常見(jiàn)的問(wèn)題,由于字符集和編碼的復(fù)雜性,開(kāi)發(fā)者可能面臨各種導(dǎo)致亂碼的情況,正確地處理中文字符集對(duì)于確保應(yīng)用程序的可靠性和國(guó)際化至關(guān)重要,本文給大家介紹了Java中文亂碼分析及解決方案,需要的朋友可以參考下2024-02-02
Java中的非對(duì)稱加密算法原理與實(shí)現(xiàn)方式
在當(dāng)今的信息時(shí)代,數(shù)據(jù)安全已經(jīng)成為了一個(gè)至關(guān)重要的問(wèn)題,加密技術(shù)作為保障信息安全的重要手段,受到了廣泛的應(yīng)用和關(guān)注,本篇文章將詳細(xì)介紹Java中的非對(duì)稱加密算法原理及其實(shí)現(xiàn)方式,需要的朋友可以參考下2023-12-12
SpringBoot中Json工具類的實(shí)現(xiàn)
本文介紹在Java項(xiàng)目中實(shí)現(xiàn)一個(gè)JSON工具類,支持對(duì)象與JSON字符串之間的轉(zhuǎn)換,并提供依賴和代碼示例便于直接應(yīng)用,感興趣的可以了解一下2024-10-10
基于SpringBoot+WebSocket實(shí)現(xiàn)網(wǎng)頁(yè)在線聊天室
文章介紹了從零構(gòu)建一套前后端分離的網(wǎng)頁(yè)聊天室系統(tǒng),涵蓋架構(gòu)設(shè)計(jì)、數(shù)據(jù)庫(kù)設(shè)計(jì)、登錄認(rèn)證、WebSocket實(shí)時(shí)消息推送及全局異常處理等核心功能,文章詳細(xì)描述了前端頁(yè)面與交互、后端技術(shù)棧及核心功能實(shí)現(xiàn)、數(shù)據(jù)庫(kù)設(shè)計(jì)等以及項(xiàng)目運(yùn)行等等內(nèi)容,需要的朋友可以參考下2026-05-05
如何將字符串、字節(jié)數(shù)組轉(zhuǎn)為輸入流
這篇文章主要介紹了如何將字符串、字節(jié)數(shù)組轉(zhuǎn)為輸入流問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring Boot 中 @Scheduled 定時(shí)任務(wù)不生效的原因及解決方法
SpringBoot中@Scheduled注解用于創(chuàng)建定時(shí)任務(wù),但有時(shí)任務(wù)可能不生效,本文介紹Spring Boot 中 @Scheduled 定時(shí)任務(wù)不生效的原因及解決方法,感興趣的朋友跟隨小編一起看看吧2025-11-11

