|
@@ -0,0 +1,210 @@
|
|
|
+package com.zskk.dicom.oss;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.common.comm.ResponseMessage;
|
|
|
+import com.aliyun.oss.common.utils.IOUtils;
|
|
|
+import com.aliyun.oss.event.ProgressEvent;
|
|
|
+import com.aliyun.oss.event.ProgressEventType;
|
|
|
+import com.aliyun.oss.event.ProgressListener;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+import com.aliyun.oss.model.Callback.CalbackBodyType;
|
|
|
+import com.zskk.dicom.config.OSSConfig;
|
|
|
+import com.zskk.dicom.monitor.config.Configs;
|
|
|
+import com.zskk.dicom.monitor.report.ErrReporter;
|
|
|
+import com.zskk.dicom.monitor.utils.ExceptionUtil;
|
|
|
+import com.zskk.dicom.response.BaseResponse;
|
|
|
+import com.zskk.dicom.response.bean.FileUploadResponseBean;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class OSSFileAndCallbackHleper implements BaseOSSHleper {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The uploading progress listener. Its progressChanged API is called by the SDK when there's an update.
|
|
|
+ */
|
|
|
+ static class PutObjectProgressListener implements ProgressListener {
|
|
|
+
|
|
|
+ private long bytesWritten = 0;
|
|
|
+ private long totalBytes = -1;
|
|
|
+ private boolean succeed = false;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void progressChanged(ProgressEvent progressEvent) {
|
|
|
+ long bytes = progressEvent.getBytes();
|
|
|
+ ProgressEventType eventType = progressEvent.getEventType();
|
|
|
+ switch (eventType) {
|
|
|
+ case TRANSFER_STARTED_EVENT:
|
|
|
+ Configs.sysLog.info("Start to upload......");
|
|
|
+ break;
|
|
|
+
|
|
|
+ case REQUEST_CONTENT_LENGTH_EVENT:
|
|
|
+ this.totalBytes = bytes;
|
|
|
+ Configs.sysLog.info(this.totalBytes + " bytes in total will be uploaded to OSS");
|
|
|
+ break;
|
|
|
+
|
|
|
+ case REQUEST_BYTE_TRANSFER_EVENT:
|
|
|
+ this.bytesWritten += bytes;
|
|
|
+ if (this.totalBytes != -1) {
|
|
|
+ int percent = (int)(this.bytesWritten * 100.0 / this.totalBytes);
|
|
|
+ Configs.sysLog.info(bytes + " bytes have been written at this time, upload progress: " +
|
|
|
+ percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")");
|
|
|
+ } else {
|
|
|
+ Configs.sysLog.info(bytes + " bytes have been written at this time, upload ratio: unknown" +
|
|
|
+ "(" + this.bytesWritten + "/...)");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TRANSFER_COMPLETED_EVENT:
|
|
|
+ this.succeed = true;
|
|
|
+ Configs.sysLog.info("Succeed to upload, " + this.bytesWritten + " bytes have been transferred in total");
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TRANSFER_FAILED_EVENT:
|
|
|
+ Configs.sysLog.info("Failed to upload, " + this.bytesWritten + " bytes have been transferred");
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isSucceed() {
|
|
|
+ return succeed;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// private static class OSSHleperHoler {
|
|
|
+// private static OSSFileAndCallbackHleper INSTANCE = new OSSFileAndCallbackHleper();
|
|
|
+// }
|
|
|
+// public static OSSFileAndCallbackHleper getInstance() {
|
|
|
+// return OSSHleperHoler.INSTANCE;
|
|
|
+// }
|
|
|
+ private OSSClient instance;
|
|
|
+ public OSSFileAndCallbackHleper(String ossEndpoint , String accessKeyId , String accessKeySecret ) {
|
|
|
+ instance = new OSSClient(ossEndpoint,accessKeyId,accessKeySecret);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public boolean exist(String md5) {
|
|
|
+ return instance.doesObjectExist(Configs.ossBucketName, md5);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean upload(File file) {
|
|
|
+ BaseResponse exception = null;
|
|
|
+ FileInputStream fis = null;
|
|
|
+ String md5 = null;
|
|
|
+ Exception error = null;
|
|
|
+ try {
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ md5 = DigestUtils.md5Hex(IOUtils.readStreamAsByteArray(fis));
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ ErrReporter.report(ExceptionUtil.getExceptionTxt(e));
|
|
|
+// e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String fileName = Configs.hospitalId + "/" + md5;
|
|
|
+
|
|
|
+ ResponseMessage responseMessage = null;
|
|
|
+ try {
|
|
|
+ if(instance.doesObjectExist(Configs.ossBucketName, fileName)) {
|
|
|
+ Configs.sysLog.info(fileName + "文件存在, 开始删除");
|
|
|
+ instance.deleteObject(Configs.ossBucketName, fileName);
|
|
|
+ Configs.sysLog.info(fileName + "文件删除成功开始上传");
|
|
|
+ }
|
|
|
+
|
|
|
+ InputStream in = new FileInputStream(file);
|
|
|
+ byte[] buffer = new byte[1024 * 50];
|
|
|
+ int bytes = 0;
|
|
|
+ int count = 0;
|
|
|
+ InputStream out = new ByteArrayInputStream(buffer);
|
|
|
+ bytes = in.read(buffer);
|
|
|
+ AppendObjectRequest appendObjectRequest = new AppendObjectRequest(Configs.ossBucketName, fileName, out);
|
|
|
+ appendObjectRequest.setPosition(0L);
|
|
|
+ AppendObjectResult appendObjectResult = instance.appendObject(appendObjectRequest);
|
|
|
+ while ((bytes = in.read(buffer)) != -1) {
|
|
|
+ appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
|
|
|
+ appendObjectRequest.setInputStream(new ByteArrayInputStream(buffer, 0, bytes));
|
|
|
+ appendObjectResult = instance.appendObject(appendObjectRequest);
|
|
|
+ Configs.sysLog.info(count++ + "");
|
|
|
+ }
|
|
|
+
|
|
|
+ Configs.sysLog.info("上传完成 responseMessage" + responseMessage);
|
|
|
+// appendObjectRequest.getResponse();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ Configs.sysLog.info("FileNotFoundException" + ExceptionUtil.getExceptionTxt(e));
|
|
|
+ return false;
|
|
|
+ } catch (IOException e) {
|
|
|
+ Configs.sysLog.info("IOException" + ExceptionUtil.getExceptionTxt(e));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Configs.sysLog.info("上传完成");
|
|
|
+// // 简单上传
|
|
|
+//
|
|
|
+// PutObjectRequest putObjectRequest = new PutObjectRequest(Configs.ossBucketName, fileName, file);
|
|
|
+// // 上传回调参数。
|
|
|
+//
|
|
|
+// Callback callback = new Callback();
|
|
|
+// long size = file.length();
|
|
|
+// String callbackBodyStr = OSSConfig.generateBodyStr(fileName, size);
|
|
|
+// // "{\\\"hospital_id\\\":\\\"" + Configs.hospitalId +"\\\",\\\"region\\\":\\\"oss-cn-beijing\\\",\\\"md5\\\":\\\""+md5+"\\\",\\\"url\\\":\\\""+OSSConfig.BASE_ALIYUN_OSS_URL + md5 +"\\\",\\\"size\\\":"+size+"}";
|
|
|
+//
|
|
|
+// callback.setCallbackUrl(Configs.ossCallbackUrl);
|
|
|
+// // 设置回调请求消息头中Host的值,如oss-cn-hangzhou.aliyuncs.com。
|
|
|
+// // callback.setCallbackHost(OSSConfig.ENDPOINT);
|
|
|
+//
|
|
|
+// Configs.sysLog.info("callbackBodyStr1" + callbackBodyStr);
|
|
|
+// callback.setCallbackBody(callbackBodyStr);
|
|
|
+// // 设置发起回调请求的Content-Type。
|
|
|
+//
|
|
|
+// callback.setCalbackBodyType(CalbackBodyType.JSON);
|
|
|
+// putObjectRequest.setCallback(callback);
|
|
|
+// if(Configs.progress) {
|
|
|
+// putObjectRequest.withProgressListener(new PutObjectProgressListener());
|
|
|
+// }
|
|
|
+// PutObjectResult putObjectResult = instance.putObject(putObjectRequest);
|
|
|
+// // 读取上传回调返回的消息内容。
|
|
|
+//
|
|
|
+// ResponseMessage responseMessage = putObjectResult.getResponse();
|
|
|
+
|
|
|
+
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ StringBuffer sb = new StringBuffer("");
|
|
|
+ try {
|
|
|
+ while (responseMessage.getContent().read(buffer) != -1) {
|
|
|
+ sb.append(new String(buffer));
|
|
|
+ }
|
|
|
+ responseMessage.getContent().close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ Configs.sysLog.warn("ResponseMessage IOException" + ExceptionUtil.getExceptionTxt(e));
|
|
|
+ ErrReporter.report(ExceptionUtil.getExceptionTxt(e));
|
|
|
+// e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String res = new String(sb);//{"code":0,"data":"dicom图像存储成功"}
|
|
|
+ Configs.sysLog.warn("sb" + sb);
|
|
|
+ Configs.sysLog.warn("aliyunoss upload res" + res);
|
|
|
+ FileUploadResponseBean fileUploadResponseBean = JSON.parseObject(res, FileUploadResponseBean.class);
|
|
|
+ // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
|
|
+ // 关闭OSSClient。
|
|
|
+ instance.shutdown();
|
|
|
+ instance = null;
|
|
|
+ if(fileUploadResponseBean.code == 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(fileUploadResponseBean.code == 1002) {
|
|
|
+ ErrReporter.report("OSS回调失败:md5" + md5 + "返回结果" + fileUploadResponseBean.toString() + "1002 文件已存在认为成功");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ ErrReporter.report("OSS回调失败:md5" + md5 + "返回结果" + fileUploadResponseBean.toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|