|
@@ -4,15 +4,14 @@ import com.aliyun.oss.ClientException;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.OSSException;
|
|
|
+import com.aliyun.oss.model.OSSObject;
|
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
|
import com.ywt.mg.core.utils.Checker;
|
|
|
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.io.*;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
@@ -101,11 +100,8 @@ public class OssSettring {
|
|
|
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());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.info("Error Message:{}\t{}" + e.getMessage(), e);
|
|
|
} finally {
|
|
|
if (ossClient != null) {
|
|
|
ossClient.shutdown();
|
|
@@ -113,4 +109,50 @@ public class OssSettring {
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * inputStream转byte数组
|
|
|
+ *
|
|
|
+ * @param inputStream 输入流对象
|
|
|
+ * @return byte数组
|
|
|
+ */
|
|
|
+ public static byte[] inputStreamToByteArray(InputStream inputStream) {
|
|
|
+ try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int num;
|
|
|
+ while ((num = inputStream.read(buffer)) != -1) {
|
|
|
+ byteArrayOutputStream.write(buffer, 0, num);
|
|
|
+ }
|
|
|
+ byteArrayOutputStream.flush();
|
|
|
+ return byteArrayOutputStream.toByteArray();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new byte[]{};
|
|
|
+ }
|
|
|
+
|
|
|
+ public byte[] readFile(String url) {
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ // 接收返回的url
|
|
|
+ try {
|
|
|
+ OSSObject ossObject = ossClient.getObject(fileBucketName, url);
|
|
|
+ logger.info("uploadFile:saveFileName:{},\turl:{}", fileBucketName, url);
|
|
|
+ return inputStreamToByteArray(ossObject.getObjectContent());
|
|
|
+ } 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 (Exception e) {
|
|
|
+ logger.info("Error Message:{}\t{}" + e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|