从阿里云OSS下载文件
时间: 2023-08-12 16:05:55 浏览: 173
从阿里云OSS下载文件的步骤如下:
首先,需要引入阿里云的依赖包,例如在Maven项目中可以添加以下依赖:
```xml
<!--阿里云oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
```
\[1\]
其次,在Controller中编写相应的方法来处理下载请求。例如:
```java
// 相应的Controller
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void downloadSource(HttpServletResponse response) {
// oss服务器上相应的文件名
// 文件名必须正确,否则会报错
List<String> fileName = new ArrayList<>();
fileName.add("文件1.doc");
fileName.add("文件2.doc");
// 生成压缩包的文件名
String zipFileName = "test";
// 调用AliYunOssUtils的方法进行批量下载
AliYunOssUtils.batchDownLoadOssFile(fileName, zipFileName, response);
}
```
\[2\]
最后,根据文件的URL链接获取OSS文件链接,并读取输入流进行下载。例如:
```java
// 截取片段代码
URL url = new URL(urllink);
// 打开到此URL的连接并返回一个用于从该连接读入的InputStream。
InputStream in = url.openStream();
String gaokuai_url = HttpUpload.uploadOSS(in, fileName);
// 如果获取到了文件链接
if (!gaokuai_url.equals("")) {
if (sb.length() > 0) {
sb.append(tag + gaokuai_url);
} else {
sb.append(gaokuai_url);
}
}
```
\[3\]
以上是从阿里云OSS下载文件的步骤。您可以根据具体的需求和代码框架进行相应的调整和实现。
#### 引用[.reference_title]
- *1* [Java调用阿里云oss下载文件](https://blog.csdn.net/Biexiansheng/article/details/121047471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [从阿里云OSS上下载文件的总结](https://blog.csdn.net/qq_38749759/article/details/107863291)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [阿里云OSS文件下载功能简易实现](https://blog.csdn.net/loongshawn/article/details/69396768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文