mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Refactor the OssController upload2 to use try with clause
This commit is contained in:
parent
0de7efabd4
commit
572b13a0b3
@ -1,6 +1,5 @@
|
|||||||
package com.alibaba.cloud.examples;
|
package com.alibaba.cloud.examples;
|
||||||
|
|
||||||
import com.alibaba.alicloud.oss.resource.OssStorageResource;
|
|
||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.common.utils.IOUtils;
|
import com.aliyun.oss.common.utils.IOUtils;
|
||||||
import com.aliyun.oss.model.OSSObject;
|
import com.aliyun.oss.model.OSSObject;
|
||||||
@ -8,6 +7,7 @@ import org.apache.commons.codec.CharEncoding;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.core.io.WritableResource;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -74,15 +74,14 @@ public class OssController {
|
|||||||
|
|
||||||
@GetMapping("/upload2")
|
@GetMapping("/upload2")
|
||||||
public String uploadWithOutputStream() {
|
public String uploadWithOutputStream() {
|
||||||
OssStorageResource ossStorageResource = new OssStorageResource(this.ossClient,
|
|
||||||
"oss://" + OssApplication.BUCKET_NAME + "/oss-test.json", true);
|
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = localFile.getInputStream();
|
try (OutputStream outputStream = ((WritableResource) this.remoteFile)
|
||||||
OutputStream outputStream = ossStorageResource.getOutputStream();
|
.getOutputStream();
|
||||||
|
InputStream inputStream = localFile.getInputStream()) {
|
||||||
StreamUtils.copy(inputStream, outputStream);
|
StreamUtils.copy(inputStream, outputStream);
|
||||||
inputStream.close();
|
}
|
||||||
outputStream.close();
|
}
|
||||||
} catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return "upload with outputStream failed";
|
return "upload with outputStream failed";
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user