刘韬 2 سال پیش
والد
کامیت
aa9f6cc5c8
1فایلهای تغییر یافته به همراه101 افزوده شده و 1 حذف شده
  1. 101 1
      DataFusion/src/com/zskk/service/DataService.java

+ 101 - 1
DataFusion/src/com/zskk/service/DataService.java

@@ -1,12 +1,21 @@
 package com.zskk.service;
 
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.Map;
 
+
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
 import com.jfinal.kit.PropKit;
 
 import okhttp3.Call;
@@ -34,7 +43,7 @@ public class DataService {
 
     private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
     
-//    private static final OSSClientImpl OSS_CLIENT = new OSSClientImpl(PropKit.get("oss_endpoint"), PropKit.get("oss_accessKey"), PropKit.get("oss_secretKey"));
+    private static final OSS OSS_CLIENT = new OSSClientBuilder().build(PropKit.get("oss_endpoint"), PropKit.get("oss_accessKey"), PropKit.get("oss_secretKey"));
     /**
      * 获取未出报告的检查列表
      * @param instutionId
@@ -103,6 +112,63 @@ public class DataService {
      public void updatePatientInfo(Map<String, String> map) {
      postWithParameters(UPDATE_PATIENT_URL, map);
      }
+     
+     /**
+      * 保存附件
+      * @param instutionId
+      * @param number
+      */
+     public void saveAnnex(Map <String,String> map, String filePath) {
+     	String newUrl = filePath.replaceAll("\\\\", "/") ;
+     	String fileNameStr[] = newUrl.split("/"); 
+         String fileName = fileNameStr[fileNameStr.length-1];
+         String fileStorePath =getFileWithUrl(newUrl, fileName);
+     	 File file =new File(fileStorePath);
+         String eTag = getFileMd5(file); 
+         String key = PropKit.get("institution_id")+"/"+eTag+".pdf"; 
+
+         Boolean eBoolean = OSS_CLIENT.doesObjectExist(PropKit.get("oss_bucketName"), key);
+         if (!eBoolean) {
+         	OSS_CLIENT.putObject(PropKit.get("oss_bucketName"), key, file);
+             Boolean bBoolean = OSS_CLIENT.doesObjectExist(PropKit.get("oss_bucketName"), key);
+             if (bBoolean) {
+             	file.delete();
+             	map.put("url", "https://annex.oss.cn-north-3.inspurcloudoss.com/"+key);
+             	map.put("name", key);
+         		postWithParameters(SAVE_ANNEX_URL, map);
+ 			}
+ 		}
+ 		
+ 	}
+     
+     private String getFileMd5(File file) {
+     	StringBuffer sb = new StringBuffer("");
+         try {
+             MessageDigest md = MessageDigest.getInstance("MD5");
+             md.update(FileUtils.readFileToByteArray(file));
+             byte b[] = md.digest();
+             int d;
+             for (int i = 0; i < b.length; i++) {
+                 d = b[i];
+                 if (d < 0) {
+                     d = b[i] & 0xff;
+                     // 与上一行效果等同
+                     // i += 256;
+                 }
+                 if (d < 16)
+                     sb.append("0");
+                 sb.append(Integer.toHexString(d));
+             }
+         	System.out.println(sb);
+
+         } catch (NoSuchAlgorithmException e) {
+             e.printStackTrace();
+         } catch (IOException e) {
+             e.printStackTrace();
+         }
+         return sb.toString();
+ 	}
+
     
     public static String postWithParameters(String url, Map<String, String> map) {
     	
@@ -149,6 +215,40 @@ public class DataService {
 
 		}
 	}
+	
+public static String getFileWithUrl(String url, String filename) {
+    	
+    	Request request = new Request.Builder()
+    	        .url(url)
+    	        .build();
+    	try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
+    	      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
+              InputStream inputStream = response.body().source().inputStream();              
+              //本地文件夹目录(下载位置)
+              String folder = PropKit.get("oss_localPath"); 
+              //下载文件保存位置
+              String savepath=folder+"/"+filename;
+              BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savepath)));
+              byte[] data=new byte[1024];
+              int len;
+              int available = inputStream.available();
+              while ((len=inputStream.read(data))!=-1){
+                  bufferedOutputStream.write(data,0,len);
+              }
+              bufferedOutputStream.flush();
+              bufferedOutputStream.close();
+              inputStream.close();
+
+    	      return savepath;
+
+    	    } catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+		    	return "";
+
+			}
+	}
+
     
     public static void main(String[] args) {
     	Map <String,String> map = new HashMap<String,String>();