|
@@ -10,7 +10,9 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
import java.io.File;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
@@ -33,7 +35,7 @@ public class OssSettring {
|
|
|
|
|
|
public static final String fileBucketName = "ywt-files";
|
|
|
|
|
|
- public String uploadFile(String localPath, String saveFileName){
|
|
|
+ public String uploadFile(String localPath, String saveFileName) {
|
|
|
// 创建OSSClient实例。
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
@@ -74,4 +76,41 @@ public class OssSettring {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
+ public String uploadFile(String saveFileName, String ossPath, ByteArrayInputStream bio) {
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ // 接收返回的url
|
|
|
+ String url = "";
|
|
|
+ try {
|
|
|
+ saveFileName = ossPath + "/" + saveFileName;
|
|
|
+ logger.info("uploadFile:saveFileName:{}", fileBucketName);
|
|
|
+ // 创建PutObjectRequest对象。
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(fileBucketName, saveFileName, bio);
|
|
|
+ // 上传文件
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+ // 获取文件url
|
|
|
+ // url过期时间
|
|
|
+ Date date = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7);
|
|
|
+ url = ossClient.generatePresignedUrl(fileBucketName, saveFileName, date).toString();
|
|
|
+ logger.info("uploadFile:url:{}", url);
|
|
|
+ return saveFileName;
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ logger.info("Caught an OSSException, which means your request made it to OSS, "
|
|
|
+ + "but was rejected with an error response for some reason.");
|
|
|
+ logger.info("Error Message:" + oe.getErrorMessage());
|
|
|
+ logger.info("Error Code:" + oe.getErrorCode());
|
|
|
+ logger.info("Request ID:" + oe.getRequestId());
|
|
|
+ logger.info("Host ID:" + oe.getHostId());
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ logger.info("Caught an ClientException, which means the client encountered "
|
|
|
+ + "a serious internal problem while trying to communicate with OSS, "
|
|
|
+ + "such as not being able to access the network.");
|
|
|
+ logger.info("Error Message:" + ce.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
}
|