|
@@ -1,95 +0,0 @@
|
|
|
-package com.zskk.dicom.oss;
|
|
|
-
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
-import org.apache.commons.codec.digest.DigestUtils;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.apache.http.util.TextUtils;
|
|
|
-
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.common.utils.IOUtils;
|
|
|
-import com.aliyun.oss.model.PutObjectRequest;
|
|
|
-import com.aliyun.oss.model.PutObjectResult;
|
|
|
-import com.zskk.dicom.config.NetCodeConfig;
|
|
|
-import com.zskk.dicom.config.OSSConfig;
|
|
|
-import com.zskk.dicom.response.BaseResponse;
|
|
|
-import com.zskk.dicom.response.FileUploadChcekResponse;
|
|
|
-import com.zskk.dicom.response.ZskkResponseFactory;
|
|
|
-import com.zskk.dicom.response.bean.FileUploadChcekResponseBean;
|
|
|
-
|
|
|
-import sun.security.provider.MD5;
|
|
|
-
|
|
|
-public class OSSFileHleper implements BaseOSSHleper {
|
|
|
-
|
|
|
- private static class OSSHleperHoler {
|
|
|
- private static OSSFileHleper INSTANCE = new OSSFileHleper();
|
|
|
- }
|
|
|
- public static OSSFileHleper getInstance() {
|
|
|
- return OSSHleperHoler.INSTANCE;
|
|
|
- }
|
|
|
- private OSSClient instance;
|
|
|
- private OSSFileHleper() {
|
|
|
- instance = new OSSClient(OSSConfig.ENDPOINT, OSSConfig.ACCESS_KEY_ID, OSSConfig.ACCESS_KEY_SECRET);
|
|
|
- }
|
|
|
- @Override
|
|
|
- public BaseResponse check(String filePath) {
|
|
|
- BaseResponse exception = null;
|
|
|
- if(TextUtils.isEmpty(filePath)) {
|
|
|
- return NetCodeConfig.FILE_PATH_IS_NULL;
|
|
|
- }
|
|
|
- File file = new File(filePath);
|
|
|
- if(!file.exists()) {
|
|
|
- return NetCodeConfig.FILE_IS_NOT_FOUND;
|
|
|
- }
|
|
|
- if(!file.isFile()) {
|
|
|
- return NetCodeConfig.FILE_IS_NOT_FILE;
|
|
|
- }
|
|
|
- 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) {
|
|
|
- error = e;
|
|
|
- }
|
|
|
- if(error != null) {
|
|
|
- return NetCodeConfig.FILE_RUNTIME.setError(error);
|
|
|
- }
|
|
|
- boolean isExist = instance.doesObjectExist(OSSConfig.BUCKET_NAME, md5);
|
|
|
- if(isExist) {
|
|
|
- return NetCodeConfig.FILE_ALIOSS_FILE_ALREADY;
|
|
|
- }
|
|
|
- return ZskkResponseFactory.getInstance().generateSuccess().setData(new FileUploadChcekResponseBean(md5,file));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public BaseResponse upload(String filePath) {
|
|
|
- BaseResponse<FileUploadChcekResponseBean> response = check(filePath);
|
|
|
- if(response.isError()) {
|
|
|
- return response;
|
|
|
- }
|
|
|
- FileUploadChcekResponseBean data = response.getData();
|
|
|
- String md5 = data.filename;
|
|
|
- File file = data.file;
|
|
|
- Exception error = null;
|
|
|
- try {
|
|
|
- instance.putObject(OSSConfig.BUCKET_NAME, md5, file);
|
|
|
- } catch (Exception e) {
|
|
|
- error = e;
|
|
|
- } finally {
|
|
|
- if(instance != null) {
|
|
|
- instance.shutdown();
|
|
|
- }
|
|
|
- }
|
|
|
- if(error != null) {
|
|
|
- return NetCodeConfig.FILE_ALIOSS_UPLOAD.setError(error);
|
|
|
- }
|
|
|
- return ZskkResponseFactory.getInstance().generateSuccess();
|
|
|
- }
|
|
|
-}
|