DataService.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package com.zskk.service;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardOpenOption;
  11. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import java.util.Random;
  16. import java.util.UUID;
  17. import org.apache.commons.io.FileUtils;
  18. import com.alibaba.fastjson.JSON;
  19. import com.alibaba.fastjson.JSONArray;
  20. import com.alibaba.fastjson.JSONObject;
  21. import com.inspurcloud.oss.client.impl.OSSClientImpl;
  22. import com.inspurcloud.oss.model.ObjectMetadata;
  23. import com.jfinal.kit.PropKit;
  24. import okhttp3.FormBody;
  25. import okhttp3.OkHttpClient;
  26. import okhttp3.Request;
  27. import okhttp3.RequestBody;
  28. import okhttp3.Response;
  29. public class DataService {
  30. private static String GET_EXAM_URL = "https://risserver3.pacsonline.cn/butt/getExam";
  31. private static String SAVE_REPORT_URL = "https://risserver3.pacsonline.cn/butt/saveReport";
  32. private static String SAVE_ANNEX_URL = "https://risserver3.pacsonline.cn/butt/saveAnnex";
  33. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  34. private static final OSSClientImpl OSS_CLIENT = new OSSClientImpl(PropKit.get("oss_endpoint"), PropKit.get("oss_accessKey"), PropKit.get("oss_secretKey"));
  35. /**
  36. * 获取未出报告的检查列表
  37. * @param instutionId
  38. * @param number
  39. */
  40. public JSONArray getExamList(Integer number) {
  41. Map <String,String> map = new HashMap<String,String>();
  42. map.put("institution_id", PropKit.get("institution_id"));
  43. map.put("num", number.toString());
  44. String content = postWithParameters(GET_EXAM_URL, map);
  45. JSONObject jsonObject = JSON.parseObject(content);
  46. if (!jsonObject.getString("msg").equals("success")) {
  47. return null;
  48. }
  49. JSONArray jsonArray = JSON.parseArray(jsonObject.getString("data"));
  50. return jsonArray;
  51. }
  52. /**
  53. * 写入报告
  54. * @param instutionId
  55. * @param number
  56. */
  57. public void saveReport(Map <String,String> map) {
  58. postWithParameters(SAVE_REPORT_URL, map);
  59. }
  60. /**
  61. * 保存附件
  62. * @param instutionId
  63. * @param number
  64. */
  65. public void saveAnnex(Map <String,String> map, String filePath) {
  66. String newUrl = filePath.replaceAll("\\\\", "/") ;
  67. String fileNameStr[] = newUrl.split("/");
  68. String fileName = fileNameStr[fileNameStr.length-1];
  69. String fileStorePath =getFileWithUrl(newUrl, fileName);
  70. File file =new File(fileStorePath);
  71. String eTag = getFileMd5(file);
  72. String key = PropKit.get("institution_id")+"/"+eTag;
  73. Boolean eBoolean = OSS_CLIENT.doesObjectExist(PropKit.get("oss_bucketName"), key);
  74. if (!eBoolean) {
  75. OSS_CLIENT.putObject(PropKit.get("oss_bucketName"), key, file);
  76. Boolean bBoolean = OSS_CLIENT.doesObjectExist(PropKit.get("oss_bucketName"), key);
  77. if (bBoolean) {
  78. file.delete();
  79. map.put("url", "https://annex.oss.cn-north-3.inspurcloudoss.com/"+key);
  80. map.put("name", key);
  81. postWithParameters(SAVE_ANNEX_URL, map);
  82. }
  83. }
  84. }
  85. private String getFileMd5(File file) {
  86. StringBuffer sb = new StringBuffer("");
  87. try {
  88. MessageDigest md = MessageDigest.getInstance("MD5");
  89. md.update(FileUtils.readFileToByteArray(file));
  90. byte b[] = md.digest();
  91. int d;
  92. for (int i = 0; i < b.length; i++) {
  93. d = b[i];
  94. if (d < 0) {
  95. d = b[i] & 0xff;
  96. // 与上一行效果等同
  97. // i += 256;
  98. }
  99. if (d < 16)
  100. sb.append("0");
  101. sb.append(Integer.toHexString(d));
  102. }
  103. System.out.println(sb);
  104. } catch (NoSuchAlgorithmException e) {
  105. e.printStackTrace();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. return sb.toString();
  110. }
  111. public static String postWithParameters(String url, Map<String, String> map) {
  112. FormBody.Builder formbody = new FormBody.Builder();
  113. for (Map.Entry<String, String> entry : map.entrySet()) {
  114. formbody.add(entry.getKey(), entry.getValue());
  115. }
  116. RequestBody requestBody = formbody.build();
  117. Request request = new Request.Builder()
  118. .url(url)
  119. .post(requestBody)
  120. .build();
  121. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  122. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  123. String content = response.body().string();
  124. return content;
  125. } catch (IOException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. return null;
  129. }
  130. }
  131. public static String getFileWithUrl(String url, String filename) {
  132. Request request = new Request.Builder()
  133. .url(url)
  134. .build();
  135. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  136. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  137. InputStream inputStream = response.body().source().inputStream();
  138. //本地文件夹目录(下载位置)
  139. String folder = PropKit.get("oss_localPath");
  140. //下载文件保存位置
  141. String savepath=folder+"/"+filename;
  142. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savepath)));
  143. byte[] data=new byte[1024];
  144. int len;
  145. int available = inputStream.available();
  146. while ((len=inputStream.read(data))!=-1){
  147. bufferedOutputStream.write(data,0,len);
  148. }
  149. bufferedOutputStream.flush();
  150. bufferedOutputStream.close();
  151. inputStream.close();
  152. return savepath;
  153. } catch (IOException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. return "";
  157. }
  158. }
  159. public static void main(String[] args) {
  160. // Map <String,String> map = new HashMap<String,String>();
  161. // map.put("institution_id", "47600001");
  162. // map.put("num", "10");
  163. // postWithParameters(GET_EXAM_URL, map);
  164. // getFileWithUrl("https://annex.oss.cn-north-3.inspurcloudoss.com/111/b.pdf");
  165. // String bucketName = "annex";
  166. // String key = "111/a.pdf";
  167. // File file = new File("/Users/liutao/Desktop/asdadsad.pdf");
  168. //
  169. // String endpoint = "oss.cn-north-3.inspurcloudoss.com";
  170. // String accessKey = "ZGEzOTY3YWQtNWI1OC00Y2MxLWJmNTgtZWVhN2M0ZjI5NTI4";
  171. // String secretKey = "YTc1NDQwZjItOWQ1OS00N2E3LTg4YmQtZjNjNjgzNzRjODQ5";
  172. // OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey);
  173. //
  174. // //简单文件上传
  175. // ossClient.putObject(bucketName, key, file);
  176. // ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, "111/a.pdf");
  177. //
  178. // System.out.println(objectMetadata);
  179. //创建OSSClient实例
  180. // Boolean aBoolean = ossClient.doesObjectExist(bucketName, key);
  181. // System.out.println(aBoolean);
  182. }
  183. }