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();
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
基于SpringBoot+WebSocket实现网页在线聊天室
文章介绍了从零构建一套前后端分离的网页聊天室系统,涵盖架构设计、数据库设计、登录认证、WebSocket实时消息推送及全局异常处理等核心功能,文章详细描述了前端页面与交互、后端技术栈及核心功能实现、数据库设计等以及项目运行等等内容,需要的朋友可以参考下2026-05-05
Spring Boot 中 @Scheduled 定时任务不生效的原因及解决方法
SpringBoot中@Scheduled注解用于创建定时任务,但有时任务可能不生效,本文介绍Spring Boot 中 @Scheduled 定时任务不生效的原因及解决方法,感兴趣的朋友跟随小编一起看看吧2025-11-11


最新评论