SpringBoot整合阿里云OSS文件上传、下载、查看、删除

x33g5p2x  于2021-12-23 转载在 Spring  
字(12.6k)|赞(0)|评价(0)|浏览(799)

SpringBoot整合阿里云OSS文件上传、下载、查看、删除

该项目源码地址:https://github.com/ggb2312/springboot-integration-examples (其中包含SpringBoot和其他常用技术的整合,配套源码以及笔记。基于最新的 SpringBoot2.1+,欢迎各位 Star)

1. 开发前准备

1.1 前置知识

  • java基础
  • SpringBoot简单基础知识

1.2 环境参数

  • 开发工具:IDEA
  • 基础环境:Maven+JDK8
  • 所用技术:SpringBoot、lombok、阿里云OSS存储服务
  • SpringBoot版本:2.1.4

1.3 涉及知识点

  • OSS简介,以及阿里云OSS控制台快速入门使用
  • SpringBoot 整合 阿里云OSS 存储服务,进行文件上传、下载、查看、删除
  • 阿里云OSS文档介绍,以及快速入门使用
  • lombok入门使用以及IDEA lombok插件安装
  • SpringMVC与AJAX前后端分离交互
  • AJAX文件异步上传

2. 使用阿里云OSS

对象存储OSS的多重冗余架构设计,为数据持久存储提供可靠保障。

2.1 创建Bucket

使用OSS,首先需要创建Bucket,Bucket翻译成中文是水桶的意思,把存储的图片资源看做是水,想要盛水必须得
有桶。
进入控制台,https://oss.console.aliyun.com/overview

创建完成后,在左侧可以看到已经创建好的Bucket:

选择Bucket后,即可看到对应的信息,如:url、消耗流量等

2.2 管理文件

可以通过在线的方式进行管理文件

2.3 阿里云OSS文档

阿里云OSS文档

右侧的开发指南说的更加详细

阿里云虽然提供了完整的文档,但是做一个完整的前后端交互的文件上传、下载、查看、删除等操作,对于小白来说还是有点难度的,所以我把自己学习OSS的步骤以及代码分享了出来,共有需要的人使用。

3. 项目初始化

3.1 创建SpringBoot项目

在Idea中File——>New——>Project

3.2 Maven依赖

导入Maven相关依赖

  1. <dependency>
  2. <groupId>com.aliyun.oss</groupId>
  3. <artifactId>aliyun-sdk-oss</artifactId>
  4. <version>2.8.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.projectlombok</groupId>
  8. <artifactId>lombok</artifactId>
  9. <version>1.18.4</version>
  10. <scope>provided</scope>
  11. </dependency>
  12. <dependency>
  13. <groupId>joda-time</groupId>
  14. <artifactId>joda-time</artifactId>
  15. <version>2.9.9</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.commons</groupId>
  19. <artifactId>commons-lang3</artifactId>
  20. <version>3.8.1</version>
  21. </dependency>

3.3 安装lombok插件

因为项目中使用了lombok的@Data注解,当然你也可以自己写get、set等方法。
File——>settings——>Plugins

然后restart IDEA即可。

4. 后端服务编写

4.1 阿里云OSS配置

在resource下新建一个application-oss.properties

  1. aliyun.endpoint=oss-cn-shanghai.aliyuncs.com
  2. aliyun.accessKeyId=你的accessKeyId
  3. aliyun.accessKeySecret=你的accessKeySecret
  4. aliyun.bucketName=gaojun-testbucket
  5. aliyun.urlPrefix=http://gaojun-testbucket.oss-cn-shanghai.aliyuncs.com/
  6. spring.servlet.multipart.max-file-size=100MB
  7. spring.servlet.multipart.max-request-size=1000MB

endpoint、bucketName、urlPrefix在OSS主面板就可以看到

accessKeyId、accessKeySecret需要在accesskeys里面查看

在java的包下新建一个config包,创建一个AliyunConfig.java

  1. package com.example.ossdemo.config;
  2. import com.aliyun.oss.OSS;
  3. import com.aliyun.oss.OSSClient;
  4. import lombok.Data;
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.annotation.PropertySource;
  9. /** * @desc */
  10. @Configuration
  11. @PropertySource(value = {"classpath:application-oss.properties"})
  12. @ConfigurationProperties(prefix = "aliyun")
  13. @Data
  14. public class AliyunConfig {
  15. private String endpoint;
  16. private String accessKeyId;
  17. private String accessKeySecret;
  18. private String bucketName;
  19. private String urlPrefix;
  20. @Bean
  21. public OSS oSSClient() {
  22. return new OSSClient(endpoint, accessKeyId, accessKeySecret);
  23. }
  24. }

4.2 后端业务

4.2.1 vo

该实体类用于后台返回给前台。

  1. package com.example.ossdemo.vo;
  2. import lombok.Data;
  3. /** * @desc 用于前后端交互的返回值 */
  4. @Data
  5. public class FileUploadResult {
  6. // 文件唯一标识
  7. private String uid;
  8. // 文件名
  9. private String name;
  10. // 状态有:uploading done error removed
  11. private String status;
  12. // 服务端响应内容,如:'{"status": "success"}'
  13. private String response;
  14. }

4.2.2 service

在service使用ossClient操作阿里云OSS,进行上传、下载、删除、查看所有文件等操作,同时可以将图片的url进行入库操作。

  1. package com.example.ossdemo.service;
  2. import com.aliyun.oss.OSS;
  3. import com.aliyun.oss.model.*;
  4. import com.example.ossdemo.config.AliyunConfig;
  5. import com.example.ossdemo.vo.FileUploadResult;
  6. import org.apache.commons.lang3.RandomUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.joda.time.DateTime;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.io.*;
  13. import java.util.List;
  14. /** * @desc */
  15. @Service
  16. public class FileUploadService {
  17. // 允许上传的格式
  18. private static final String[] IMAGE_TYPE = new String[]{".bmp", ".jpg",
  19. ".jpeg", ".gif", ".png"};
  20. @Autowired
  21. private OSS ossClient;
  22. @Autowired
  23. private AliyunConfig aliyunConfig;
  24. /** * @author lastwhisper * @desc 文件上传 * 文档链接 https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.11186623.6.749.11987a7dRYVSzn * @email gaojun56@163.com */
  25. public FileUploadResult upload(MultipartFile uploadFile) {
  26. // 校验图片格式
  27. boolean isLegal = false;
  28. for (String type : IMAGE_TYPE) {
  29. if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(),
  30. type)) {
  31. isLegal = true;
  32. break;
  33. }
  34. }
  35. //封装Result对象,并且将文件的byte数组放置到result对象中
  36. FileUploadResult fileUploadResult = new FileUploadResult();
  37. if (!isLegal) {
  38. fileUploadResult.setStatus("error");
  39. return fileUploadResult;
  40. }
  41. //文件新路径
  42. String fileName = uploadFile.getOriginalFilename();
  43. String filePath = getFilePath(fileName);
  44. // 上传到阿里云
  45. try {
  46. ossClient.putObject(aliyunConfig.getBucketName(), filePath, new
  47. ByteArrayInputStream(uploadFile.getBytes()));
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. //上传失败
  51. fileUploadResult.setStatus("error");
  52. return fileUploadResult;
  53. }
  54. fileUploadResult.setStatus("done");
  55. fileUploadResult.setResponse("success");
  56. //this.aliyunConfig.getUrlPrefix() + filePath 文件路径需要保存到数据库
  57. fileUploadResult.setName(this.aliyunConfig.getUrlPrefix() + filePath);
  58. fileUploadResult.setUid(String.valueOf(System.currentTimeMillis()));
  59. return fileUploadResult;
  60. }
  61. /** * @author lastwhisper * @desc 生成路径以及文件名 例如://images/2019/04/28/15564277465972939.jpg * @email gaojun56@163.com */
  62. private String getFilePath(String sourceFileName) {
  63. DateTime dateTime = new DateTime();
  64. return "images/" + dateTime.toString("yyyy")
  65. + "/" + dateTime.toString("MM") + "/"
  66. + dateTime.toString("dd") + "/" + System.currentTimeMillis() +
  67. RandomUtils.nextInt(100, 9999) + "." +
  68. StringUtils.substringAfterLast(sourceFileName, ".");
  69. }
  70. /** * @author lastwhisper * @desc 查看文件列表 * 文档链接 https://help.aliyun.com/document_detail/84841.html?spm=a2c4g.11186623.2.13.3ad5b5ddqxWWRu#concept-84841-zh * @email gaojun56@163.com */
  71. public List<OSSObjectSummary> list() {
  72. // 设置最大个数。
  73. final int maxKeys = 200;
  74. // 列举文件。
  75. ObjectListing objectListing = ossClient.listObjects(new ListObjectsRequest(aliyunConfig.getBucketName()).withMaxKeys(maxKeys));
  76. List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
  77. return sums;
  78. }
  79. /** * @author lastwhisper * @desc 删除文件 * 文档链接 https://help.aliyun.com/document_detail/84842.html?spm=a2c4g.11186623.6.770.4f9474b4UYlCtr * @email gaojun56@163.com */
  80. public FileUploadResult delete(String objectName) {
  81. // 根据BucketName,objectName删除文件
  82. ossClient.deleteObject(aliyunConfig.getBucketName(), objectName);
  83. FileUploadResult fileUploadResult = new FileUploadResult();
  84. fileUploadResult.setName(objectName);
  85. fileUploadResult.setStatus("removed");
  86. fileUploadResult.setResponse("success");
  87. return fileUploadResult;
  88. }
  89. /** * @author lastwhisper * @desc 下载文件 * 文档链接 https://help.aliyun.com/document_detail/84823.html?spm=a2c4g.11186623.2.7.37836e84ZIuZaC#concept-84823-zh * @email gaojun56@163.com */
  90. public void exportOssFile(OutputStream os, String objectName) throws IOException {
  91. // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
  92. OSSObject ossObject = ossClient.getObject(aliyunConfig.getBucketName(), objectName);
  93. // 读取文件内容。
  94. BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
  95. BufferedOutputStream out = new BufferedOutputStream(os);
  96. byte[] buffer = new byte[1024];
  97. int lenght = 0;
  98. while ((lenght = in.read(buffer)) != -1) {
  99. out.write(buffer, 0, lenght);
  100. }
  101. if (out != null) {
  102. out.flush();
  103. out.close();
  104. }
  105. if (in != null) {
  106. in.close();
  107. }
  108. }
  109. }

4.2.3 controller

controller进行接收用户请求

  1. package com.example.ossdemo.controller;
  2. import com.aliyun.oss.model.OSSObjectSummary;
  3. import com.example.ossdemo.service.FileUploadService;
  4. import com.example.ossdemo.vo.FileUploadResult;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.net.URLEncoder;
  19. import java.util.List;
  20. /** * @desc */
  21. @Controller
  22. public class FileUploadController {
  23. @Autowired
  24. private FileUploadService fileUploadService;
  25. /** * @desc 文件上传到oss * @return FileUploadResult * @Param uploadFile */
  26. @RequestMapping("file/upload")
  27. @ResponseBody
  28. public FileUploadResult upload(@RequestParam("file") MultipartFile uploadFile)
  29. throws Exception {
  30. return this.fileUploadService.upload(uploadFile);
  31. }
  32. /** * @return FileUploadResult * @desc 根据文件名删除oss上的文件 * http://localhost:8080/file/delete?fileName=images/2019/04/28/1556429167175766.jpg * @Param objectName */
  33. @RequestMapping("file/delete")
  34. @ResponseBody
  35. public FileUploadResult delete(@RequestParam("fileName") String objectName)
  36. throws Exception {
  37. return this.fileUploadService.delete(objectName);
  38. }
  39. /** * @author lastwhisper * @desc 查询oss上的所有文件 * http://localhost:8080/file/list * @return List<OSSObjectSummary> * @Param */
  40. @RequestMapping("file/list")
  41. @ResponseBody
  42. public List<OSSObjectSummary> list()
  43. throws Exception {
  44. return this.fileUploadService.list();
  45. }
  46. /** * @author lastwhisper * @desc 根据文件名下载oss上的文件 * @return * @Param objectName */
  47. @RequestMapping("file/download")
  48. @ResponseBody
  49. public void download(@RequestParam("fileName") String objectName, HttpServletResponse response) throws IOException {
  50. //通知浏览器以附件形式下载
  51. response.setHeader("Content-Disposition",
  52. "attachment;filename=" + new String(objectName.getBytes(), "ISO-8859-1"));
  53. this.fileUploadService.exportOssFile(response.getOutputStream(),objectName);
  54. }
  55. }

5. 前端页面编写与测试

5.1 文件上传页面

使用ajax异步文件上传到后端对接的OSS上。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>oss文件上传</title>
  6. <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script>
  7. <script> function uploadfile() { $("#fileTypeError").html(''); //获得文件名称 var fileName = $('#file_upload').val(); //截取文件类型,如(.jpg)                 var fileType = fileName.substr(fileName.length - 4, fileName.length); if (fileType == '.bmp' || fileType == '.jpg' || fileType == '.jpeg' || fileType == '.gif' || fileType == '.png') {     //验证文件类型,此处验证也可使用正则 $.ajax({ url: 'file/upload',//上传地址 type: 'POST', cache: false, data: new FormData($('#uploadForm')[0]),//表单数据 processData: false, contentType: false, success: function (rtn) { if (rtn.status == 'error') { $("#fileTypeError").html('*上传文件类型错误,支持类型: .bmp .jpg .jpeg .gif .png');  //根据后端返回值,回显错误信息 } else { $('div').append('<img src="' + rtn.name + '" style="width: 300px;height: 300px"></img>') } } }); } else { $("#fileTypeError").html('*上传文件类型错误,支持类型: .bmp .jpg .jpeg .gif .png'); } } </script>
  8. </head>
  9. <body>
  10. <form id="uploadForm" enctype="multipart/form-data">  <!-- 声明文件上传 -->
  11. <input id="file_upload" type="file" name="file"/>  <!-- 定义change事件,选择文件后触发 -->
  12. <br/><span style="color: red" id="fileTypeError"></span>    <!-- 文件类型错误回显,此处通过前后端两次验证文件类型 -->
  13. <br/><input type="button" onclick="uploadfile()" value="上传">
  14. </form>
  15. <div></div>
  16. </body>
  17. </html>

效果展示:

5.2 文件管理页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>oss文件管理</title>
  6. <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script>
  7. <script type="text/javascript"> var pre = 'https://gaojun-testbucket.oss-cn-shanghai.aliyuncs.com/'; $(function () { listfile(); }); //文件列表 function listfile() { $.ajax({ url: "http://localhost:8080/file/list", type: 'POST', success: function (rtn) { console.log(rtn.length); for (var i = 0; i < rtn.length; i++) { $('div').append('<img src="' + pre + rtn[i].key + '" style="width: 300px;height: 300px; padding: 10px" οndblclick="deletefile(this.src)" οnclick="downloadfile(this.src)"></img>') } } }); } //文件下载 function downloadfile(src) { var fileName = src.split(pre)[1]; window.location.href = "http://localhost:8080/file/download?fileName=" + fileName; } //文件删除 function deletefile(src) { var fileName = src.split(pre)[1]; var param = {fileName: fileName}; $.ajax({ url: "http://localhost:8080/file/delete", data: param, success: function () { alert('删除成功',fileName); //删除页面 location.reload(); } }); } </script>
  8. </head>
  9. <body>
  10. 单击下载oss上的图片、双击删除oss上的图片<br>
  11. <div>
  12. </div>
  13. </body>
  14. </html>

效果展示:

刚才上传了一张照片,可以立马看到

单击页面即可下载图片

双击即可删除图片:

相关文章