DataService.java 7.0 KB

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