Commit ef6d3136 by 邓实川

oss下载文件

parent f79ce8e4
......@@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -105,6 +106,21 @@ public class OSSController {
}
/**
* 下载
*/
@GetMapping(value = "/download")
@ApiOperation(value = "通过url直接下载文件", httpMethod = "GET", notes = "接口发布说明")
public Result<String> download(@CurrentUser UserBean userBean, @RequestParam String url,
@RequestParam String savePath) {
try {
oss.download(url, savePath);
} catch (Exception e) {
return ResultUtil.error("下载失败");
}
return ResultUtil.data("下载成功");
}
/**
* 获取私密文件url(设置为10分钟有效)
*/
@PostMapping(value = "/getpri")
......
......@@ -19,6 +19,9 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GetObjectRequest;
......@@ -484,4 +487,74 @@ public class OSSUtil {
* // 关闭OSSClient。 ossClient.shutdown();
*/
public void download(String url, String savePath) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// // https://ram.console.aliyun.com 创建RAM账号。
// String accessKeyId = "LTAI4FuaShJWQ1dggsFWG5CC";
// String accessKeySecret = "EJ6qToT4T4u0B5Rb6qrta9WkyGHvGR";
// String bucketName = "8time-v2";
// String objectName = "8timer2.0/117/test/测试.pdf";
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// String url = "http://8time-v2.oss-cn-shenzhen.aliyuncs.com/8timer2.0/310/qyxx/filepath/158872957193802f4a85a-9dd9-483a-9049-86fac5c73bd5.pdf?Expires=4747952308&OSSAccessKeyId=LTAI4FuaShJWQ1dggsFWG5CC&Signature=z%2FNIX0RrxnLuBLAS9olPpxxsCrI%3D";
try {
// 带进度条的下载。
ossClient.getObject(new GetObjectRequest(new URL(url), null), new File(savePath));
// new File("C:\\Users\\Lenovo\\Desktop\\8小时\\test2.pdf"));
// // 带进度条的下载。
// ossClient.getObject(new GetObjectRequest(bucketName, objectName).<GetObjectRequest>withProgressListener(
// new GetObjectProgressListener()), new File("C:\\Users\\Lenovo\\Desktop\\8小时\\test.pdf"));
} catch (Exception e) {
e.printStackTrace();
}
// 关闭OSSClient。
ossClient.shutdown();
}
static class GetObjectProgressListener implements ProgressListener {
private long bytesRead = 0;
private long totalBytes = -1;
private boolean succeed = false;
@Override
public void progressChanged(ProgressEvent progressEvent) {
long bytes = progressEvent.getBytes();
ProgressEventType eventType = progressEvent.getEventType();
switch (eventType) {
case TRANSFER_STARTED_EVENT:
System.out.println("Start to download......");
break;
case RESPONSE_CONTENT_LENGTH_EVENT:
this.totalBytes = bytes;
System.out.println(this.totalBytes + " bytes in total will be downloaded to a local file");
break;
case RESPONSE_BYTE_TRANSFER_EVENT:
this.bytesRead += bytes;
if (this.totalBytes != -1) {
int percent = (int) (this.bytesRead * 100.0 / this.totalBytes);
System.out.println(bytes + " bytes have been read at this time, download progress: " + percent
+ "%(" + this.bytesRead + "/" + this.totalBytes + ")");
} else {
System.out.println(bytes + " bytes have been read at this time, download ratio: unknown" + "("
+ this.bytesRead + "/...)");
}
break;
case TRANSFER_COMPLETED_EVENT:
this.succeed = true;
System.out.println("Succeed to download, " + this.bytesRead + " bytes have been transferred in total");
break;
case TRANSFER_FAILED_EVENT:
System.out.println("Failed to download, " + this.bytesRead + " bytes have been transferred");
break;
default:
break;
}
}
public boolean isSucceed() {
return succeed;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment