|
|
@@ -1,28 +1,30 @@
|
|
|
package com.izouma.awesomeadmin.service;
|
|
|
|
|
|
+import com.izouma.awesomeadmin.util.ImagesUtil;
|
|
|
+import com.izouma.awesomeadmin.util.PropertiesFileLoader;
|
|
|
+import com.ksyun.ks3.dto.CannedAccessControlList;
|
|
|
+import com.ksyun.ks3.dto.ObjectMetadata;
|
|
|
+import com.ksyun.ks3.http.HttpClientConfig;
|
|
|
+import com.ksyun.ks3.service.Ks3;
|
|
|
+import com.ksyun.ks3.service.Ks3Client;
|
|
|
+import com.ksyun.ks3.service.Ks3ClientConfig;
|
|
|
+import com.ksyun.ks3.service.request.PutObjectRequest;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
import java.io.InputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.Random;
|
|
|
|
|
|
-import org.apache.log4j.Logger;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.model.OSSObject;
|
|
|
-import com.aliyun.oss.model.ObjectMetadata;
|
|
|
-import com.aliyun.oss.model.PutObjectResult;
|
|
|
-import com.izouma.awesomeadmin.util.ImagesUtil;
|
|
|
-import com.izouma.awesomeadmin.util.PropertiesFileLoader;
|
|
|
-
|
|
|
@Service
|
|
|
public class OSSFileService {
|
|
|
|
|
|
/**
|
|
|
* 日志对象
|
|
|
*/
|
|
|
- private static Logger logger = Logger.getLogger(OSSFileService.class);
|
|
|
+ private static Logger logger = Logger.getLogger(OSSFileService.class);
|
|
|
private static final String projectName = PropertiesFileLoader.getProperties("projectname");
|
|
|
|
|
|
public String upload(InputStream fin, String path) {
|
|
|
@@ -31,26 +33,27 @@ public class OSSFileService {
|
|
|
}
|
|
|
path = projectName + "/" + path;
|
|
|
|
|
|
- String aliid = PropertiesFileLoader.getDefaultProperties("aliossid", "");
|
|
|
- String alikey = PropertiesFileLoader.getDefaultProperties("aliosskey", "");
|
|
|
- String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliossendpoit", "");
|
|
|
- String bucketname = PropertiesFileLoader.getDefaultProperties("alibucketname", "");
|
|
|
- logger.info(String.format("OSS上传:\naliid: %s\nalikey: %s\naliossendpoit: %s\nbucketname: %s\npath: %s", aliid, alikey, aliossendpoit, bucketname, path));
|
|
|
|
|
|
- OSSClient client = new OSSClient(aliossendpoit, aliid, alikey);
|
|
|
- OSSObject object = null;
|
|
|
+ Ks3ClientConfig config = new Ks3ClientConfig();
|
|
|
+ config.setEndpoint("ks3-cn-shanghai.ksyun.com"); //此处以北京region为例
|
|
|
+ config.setDomainMode(false);
|
|
|
+ config.setProtocol(Ks3ClientConfig.PROTOCOL.https);
|
|
|
+
|
|
|
+ config.setPathStyleAccess(false);
|
|
|
+ HttpClientConfig hconfig = new HttpClientConfig();
|
|
|
+ config.setHttpClientConfig(hconfig);
|
|
|
try {
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- metadata.setContentDisposition("inline;filename=" + path.substring(path.lastIndexOf("/") + 1));
|
|
|
- client.putObject(bucketname, path, fin, metadata);
|
|
|
- object = client.getObject(bucketname, path);
|
|
|
+ Ks3 client = new Ks3Client("AKLTwzz8bGSSRIeiu0Adw5IRTA", "OKJAHrY+bpm96y66BazNWvmUtgybRldvcOPOCnz9SxCnxT2dm7/NW5v+skiRPVyO3A==", config);
|
|
|
+ ObjectMetadata meta = new ObjectMetadata();
|
|
|
+ PutObjectRequest request = new PutObjectRequest("yqjjc", path, fin, meta);
|
|
|
+ request.setCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
+ client.putObject(request);
|
|
|
+ logger.info("OSS上传成功");
|
|
|
+ System.out.println("https://ks3-cn-shanghai.ksyun.com/yqjjc/" + path);
|
|
|
+ return "https://ks3-cn-shanghai.ksyun.com/" + path;
|
|
|
} catch (Exception r) {
|
|
|
logger.error("OSS上传异常:", r);
|
|
|
}
|
|
|
- // 如果不设置content-length, 默认为chunked编码。
|
|
|
- if (object != null) {
|
|
|
- return getFullPath(path);
|
|
|
- }
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@@ -77,7 +80,8 @@ public class OSSFileService {
|
|
|
randomCode.append(Integer.toString(random.nextInt(36), 36));
|
|
|
}
|
|
|
try {
|
|
|
- String path = String.format("images/%s-%s.jpg", new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()), randomCode);
|
|
|
+ String path = String.format("images/%s-%s.jpg", new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss")
|
|
|
+ .format(new Date()), randomCode);
|
|
|
return upload(ImagesUtil.GenerateImage(base64), path);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|