Java调⽤阿⾥云OSS下载⽂件
1、准备⼯作
2、项⽬需求
⾸先需要引⼊阿⾥云的依赖包,如下所⽰:
中华人民共和国行政处罚法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 ller;
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.del.GetObjectRequest;
15 import com.del.OSSObject;
16
17 @Controller
18 @RequestMapping(value = "/oss")
19public class ServiceMattersOSSController {
20
21// ⽇志记录器
22private static final Logger logger = Logger(ServiceMattersOSSController.class);
23
24// 地域节点
25 @Value("${aliyun.dpoint}")
26private String endpoint;
27
28// 创建accesskey⽣成的keyid
29 @Value("${aliyun.oss.file.keyid}")
30private String accessKeyId;
31
32// 创建accesskey⽣成的secret
33 @Value("${aliyun.oss.file.keysecret}")
34private String accessKeySecret;
35
36// 创建bucket时输⼊的名称
37 @Value("${aliyun.oss.file.bucketname}")
38private String bucketName;
postcode39
40// 阿⾥云OSS规范,Object绝对路径名前⾯不需要加斜杠
41 @Value("${aliyun.oss.file.folder}")
42private String folder;
43
44/**
45 * 下载⽂件到本地路径
46 *
47 * fwsx库的clwj字段取字段。
48 *
49 * @param fileName
50*/
51 @RequestMapping(value = "/downOSSFileLocal")
52 @ResponseBody
53public void downOSSFileLocal(String fileName) {
54 // 创建OSSClient实例
56 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
57
59 OSSObject object = Object(bucketName, folder + fileName);63// 下载Object到本地⽂件,并保存到指定的本地路径中。如果指定的本地⽂件存在会覆盖,不存在则新建。64// 如果未指定本地路径,则下载后的⽂件默认保存到⽰例程序所属项⽬对应本地路径中。
65 Object(new GetObjectRequest(bucketName, folder + fileName),
66new File("/data/apache/apache-tomcat-9.0.36/webapps/" + fileName));
67// 关闭OSSClient。
68 ossClient.shutdown();70 }
71
72 }
如果只是想将oss⽂件响应给前端浏览器,可以使⽤下⾯的案例。
1 ller;
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.del.GetObjectRequest;
20 import com.del.OSSObject;
21
22 @Controller
23 @RequestMapping(value = "/oss")
24public class ServiceMattersOSSController {
25
26// ⽇志记录器
27private static final Logger logger = Logger(ServiceMattersOSSController.class);
28java下载过程
29// 地域节点
30 @Value("${aliyun.dpoint}")
31private String endpoint;
32
33// 创建accesskey⽣成的keyid
34 @Value("${aliyun.oss.file.keyid}")
35private String accessKeyId;
36
37// 创建accesskey⽣成的secret
38 @Value("${aliyun.oss.file.keysecret}")
39private String accessKeySecret;
40
41// 创建bucket时输⼊的名称
42 @Value("${aliyun.oss.file.bucketname}")
43private String bucketName;
44
45// 阿⾥云OSS规范,Object绝对路径名前⾯不需要加斜杠
46 @Value("${aliyun.oss.file.folder}")
47private String folder;
48
49/**
50 * 下载OSS服务器的⽂件
51 *
52 * @param fileName
53 * @param response
54*/
55 @RequestMapping(value = "/downOSSFile")
56 @ResponseBody汇编步骤
57public 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 = Object(bucketName, folder + fileName); 68try {
69 set();
70 response.setCharacterEncoding("utf-8");
71 response.setContentType("application/x-msdownload");
72 response.addHeader("Content-Disposition",
73"attachment;filename=" + new Bytes("gb2312"), "ISO8859-1"));
74
75 input = new ObjectContent());
76byte[] buffBytes = new byte[1024];
77 outputStream = OutputStream();
78int read = 0;
79while ((read = ad(buffBytes)) != -1) {
80 outputStream.write(buffBytes, 0, read);
plsqldeveloper破解81 }
82 outputStream.flush();
83// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求⽆连接可⽤,程序⽆法正常⼯作。
84 ossObject.close();
85 } catch (IOException ex) {
86 ex.printStackTrace();
87 } finally {
88try {
89if (outputStream != null) {
90 outputStream.close();
91 }
92if (input != null) {
93 input.close();
94 }
95 } catch (IOException e) {
96 e.printStackTrace();
97 }
98 }
99 ossClient.shutdown();101 }
轮播图js原生代码102
103
104 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论