SpringBoot上傳下載文件+oss實例
更新時間:2024年04月19日 15:52:44 作者:偷代碼的貓
這篇文章主要介紹了SpringBoot上傳下載文件+oss實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合MinIO實現(xiàn)文件存儲系統(tǒng)的代碼示例
在現(xiàn)代的應(yīng)用程序中,文件存儲和管理是一個常見的需求,MinIO是一個開源的對象存儲系統(tǒng),與Spring?Boot框架結(jié)合使用,可以快速構(gòu)建高性能的文件存儲系統(tǒng),本文將介紹如何使用Spring?Boot和MinIO來實現(xiàn)文件存儲系統(tǒng)2023-06-06解決Java Redis刪除HashMap中的key踩到的坑
這篇文章主要介紹了解決Java Redis刪除HashMap中的key踩到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java使用組件編寫窗口實現(xiàn)網(wǎng)上文件下載
這篇文章主要為大家詳細介紹了Java使用組件編寫窗口實現(xiàn)網(wǎng)上文件下載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02SpringBoot實現(xiàn)文件在線預(yù)覽功能的全過程
我們開發(fā)業(yè)務(wù)系統(tǒng)的時候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2021-11-11Spring Cloud學習教程之DiscoveryClient的深入探究
這篇文章主要給大家介紹了關(guān)于Spring Cloud學習教程之DiscoveryClient的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-04-04springboot學習之構(gòu)建簡單項目搭建步驟詳解
這篇文章主要介紹了springboot學習之構(gòu)建簡單項目搭建步驟詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10