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

update examples

This commit is contained in:
fangjian0423
2019-10-31 15:26:00 +08:00
parent 591eb9ca37
commit a939bb3047
18 changed files with 208 additions and 17 deletions

View File

@@ -12,14 +12,17 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/**
* OSS Application
* OSS Application.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@SpringBootApplication
public class OssApplication {
public static final String BUCKET_NAME = "spring-cloud-alibaba-test";
/**
* Bucket Name of OSS Example.
*/
public static final String BUCKET_NAME = "spring-cloud-alibaba";
public static void main(String[] args) throws URISyntaxException {
SpringApplication.run(OssApplication.class, args);
@@ -31,6 +34,7 @@ public class OssApplication {
}
class AppRunner implements ApplicationRunner {
@Autowired
private OSS ossClient;
@@ -46,6 +50,7 @@ public class OssApplication {
System.exit(-1);
}
}
}
}

View File

@@ -1,5 +1,23 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.examples;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import com.aliyun.oss.OSS;
@@ -10,12 +28,13 @@ 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;
/**
* OSS Controller
* OSS Controller.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@@ -25,8 +44,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 +67,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 +89,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";
}
}