1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge pull request #900 from lichen782/master

Support OssStorage Resource as WritableResource and add related unittest cases
This commit is contained in:
format
2019-09-17 10:11:54 +08:00
committed by GitHub
8 changed files with 523 additions and 27 deletions

View File

@@ -1,18 +1,20 @@
package com.alibaba.cloud.examples;
import java.nio.charset.Charset;
import org.apache.commons.codec.CharEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.IOUtils;
import com.aliyun.oss.model.OSSObject;
import org.apache.commons.codec.CharEncoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
/**
* OSS Controller
@@ -25,8 +27,11 @@ public class OssController {
@Autowired
private OSS ossClient;
@Value("classpath:/oss-test.json")
private Resource localFile;
@Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json")
private Resource file;
private Resource remoteFile;
@GetMapping("/upload")
public String upload() {
@@ -45,7 +50,7 @@ public class OssController {
public String fileResource() {
try {
return "get file resource success. content: " + StreamUtils.copyToString(
file.getInputStream(), Charset.forName(CharEncoding.UTF_8));
remoteFile.getInputStream(), Charset.forName(CharEncoding.UTF_8));
}
catch (Exception e) {
e.printStackTrace();
@@ -67,4 +72,20 @@ public class OssController {
}
}
@GetMapping("/upload2")
public String uploadWithOutputStream() {
try {
try (OutputStream outputStream = ((WritableResource) this.remoteFile)
.getOutputStream();
InputStream inputStream = localFile.getInputStream()) {
StreamUtils.copy(inputStream, outputStream);
}
}
catch (Exception ex) {
ex.printStackTrace();
return "upload with outputStream failed";
}
return "upload success";
}
}