参考:https://blog.csdn.net/haiyanghan/article/details/106771047
阿里云官方指导:https://help.aliyun.com/document_detail/84822.html
1、准备工作
具体细节参考https://blog.csdn.net/haiyanghan/article/details/106771047。
2、项目需求
我这里只需要根据文件名称把文件从oss下载下来即可,参考阿里云官网指导:https://help.aliyun.com/document_detail/84822.html
首先需要引入阿里云的依赖包,如下所示:
1 <!--阿里云oss --> 2 <dependency> 3 <groupId>com.aliyun.oss</groupId> 4 <artifactId>aliyun-sdk-oss</artifactId> 5 <version>3.10.2</version> 6 </dependency>
如果只是想将oss文件下载到服务器的磁盘里面,可以使用下面的案例。需要注意的就是OSS的文件目录下面的文件,指定下载路径的时候一定要写正常,不然会报NoSuchKey的异常信息。
1 package com.controller; 2 3 import java.io.File; 4 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.ResponseBody; 11 12 import com.aliyun.oss.OSS; 13 import com.aliyun.oss.OSSClientBuilder; 14 import com.aliyun.oss.model.GetObjectRequest; 15 import com.aliyun.oss.model.OSSObject; 16 17 @Controller 18 @RequestMapping(value = "/oss") 19 public class ServiceMattersOSSController { 20 21 // 日志记录器 22 private static final Logger logger = LoggerFactory.getLogger(ServiceMattersOSSController.class); 23 24 // 地域节点 25 @Value("${aliyun.oss.file.endpoint}") 26 private String endpoint; 27 28 // 创建accesskey生成的keyid 29 @Value("${aliyun.oss.file.keyid}") 30 private String accessKeyId; 31 32 // 创建accesskey生成的secret 33 @Value("${aliyun.oss.file.keysecret}") 34 private String accessKeySecret; 35 36 // 创建bucket时输入的名称 37 @Value("${aliyun.oss.file.bucketname}") 38 private String bucketName; 39 40 // 阿里云OSS规范,Object绝对路径名前面不需要加斜杠 41 @Value("${aliyun.oss.file.folder}") 42 private String folder; 43 44 /** 45 * 下载文件到本地路径 46 * 47 * fwsx库的clwj字段取字段。 48 * 49 * @param fileName 50 */ 51 @RequestMapping(value = "/downOSSFileLocal") 52 @ResponseBody 53 public void downOSSFileLocal(String fileName) { 54 // 创建OSSClient实例 56 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); 57 59 OSSObject object = ossClient.getObject(bucketName, folder + fileName);63 // 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。 64 // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。 65 ossClient.getObject(new GetObjectRequest(bucketName, folder + fileName), 66 new File("/data/apache/apache-tomcat-9.0.36/webapps/" + fileName)); 67 // 关闭OSSClient。 68 ossClient.shutdown();70 } 71 72 }
如果只是想将oss文件响应给前端浏览器,可以使用下面的案例。
1 package com.controller; 2 3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.OutputStream; 7 8 import javax.servlet.http.HttpServletResponse; 9 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.beans.factory.annotation.Value; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.ResponseBody; 16 17 import com.aliyun.oss.OSS; 18 import com.aliyun.oss.OSSClientBuilder; 19 import com.aliyun.oss.model.GetObjectRequest; 20 import com.aliyun.oss.model.OSSObject; 21 22 @Controller 23 @RequestMapping(value = "/oss") 24 public class ServiceMattersOSSController { 25 26 // 日志记录器 27 private static final Logger logger = LoggerFactory.getLogger(ServiceMattersOSSController.class); 28 29 // 地域节点 30 @Value("${aliyun.oss.file.endpoint}") 31 private String endpoint; 32 33 // 创建accesskey生成的keyid 34 @Value("${aliyun.oss.file.keyid}") 35 private String accessKeyId; 36 37 // 创建accesskey生成的secret 38 @Value("${aliyun.oss.file.keysecret}") 39 private String accessKeySecret; 40 41 // 创建bucket时输入的名称 42 @Value("${aliyun.oss.file.bucketname}") 43 private String bucketName; 44 45 // 阿里云OSS规范,Object绝对路径名前面不需要加斜杠 46 @Value("${aliyun.oss.file.folder}") 47 private String folder; 48 49 /** 50 * 下载OSS服务器的文件 51 * 52 * @param fileName 53 * @param response 54 */ 55 @RequestMapping(value = "/downOSSFile") 56 @ResponseBody 57 public void downOSSFile(String fileName, HttpServletResponse response) { 59 BufferedInputStream input = null; 60 OutputStream outputStream = null; 61 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); 64 OSSObject ossObject = ossClient.getObject(bucketName, folder + fileName); 68 try { 69 response.reset(); 70 response.setCharacterEncoding("utf-8"); 71 response.setContentType("application/x-msdownload"); 72 response.addHeader("Content-Disposition", 73 "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1")); 74 75 input = new BufferedInputStream(ossObject.getObjectContent()); 76 byte[] buffBytes = new byte[1024]; 77 outputStream = response.getOutputStream(); 78 int read = 0; 79 while ((read = input.read(buffBytes)) != -1) { 80 outputStream.write(buffBytes, 0, read); 81 } 82 outputStream.flush(); 83 // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。 84 ossObject.close(); 85 } catch (IOException ex) { 86 ex.printStackTrace(); 87 } finally { 88 try { 89 if (outputStream != null) { 90 outputStream.close(); 91 } 92 if (input != null) { 93 input.close(); 94 } 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 } 99 ossClient.shutdown();101 } 102 103 104 }