DataService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.util.HashMap;
  8. import java.util.Map;
  9. import java.io.OutputStream;
  10. import org.apache.commons.net.ftp.FTPClient;
  11. import org.apache.commons.net.ftp.FTPReply;
  12. import com.alibaba.fastjson.JSON;
  13. import com.alibaba.fastjson.JSONArray;
  14. import com.alibaba.fastjson.JSONObject;
  15. import com.jfinal.kit.PropKit;
  16. import okhttp3.FormBody;
  17. import okhttp3.OkHttpClient;
  18. import okhttp3.Request;
  19. import okhttp3.RequestBody;
  20. import okhttp3.Response;
  21. import com.amazonaws.AmazonServiceException;
  22. import com.amazonaws.SdkClientException;
  23. import com.amazonaws.auth.AWSCredentialsProvider;
  24. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  25. import com.amazonaws.auth.BasicAWSCredentials;
  26. import com.amazonaws.client.builder.AwsClientBuilder;
  27. import com.amazonaws.services.s3.AmazonS3;
  28. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  29. import com.amazonaws.services.s3.model.CannedAccessControlList;
  30. import com.amazonaws.services.s3.model.PutObjectRequest;
  31. public class DataService {
  32. private static String GET_EXAM_URL = "https://risserver3.pacsonline.cn/butt/getExam";
  33. private static String SAVE_REPORT_URL = "https://risserver3.pacsonline.cn/butt/saveReport";
  34. private static String CREATE_REGISTER_URL = "https://risserver3.pacsonline.cn/butt/register";
  35. private static String SAVE_FILE_URL = "https://risserver3.pacsonline.cn/butt/saveFile";
  36. private static String SAVE_ANNEX_URL = "https://risserver3.pacsonline.cn/butt/saveAnnex";
  37. private static String UPDATE_PATIENT_URL = "https://risserver3.pacsonline.cn/butt/saveExam";
  38. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  39. /**
  40. * 获取未出报告的检查列表
  41. * @param instutionId
  42. * @param number
  43. */
  44. public JSONArray getExamList(Integer number) {
  45. Map <String,String> map = new HashMap<String,String>();
  46. map.put("institution_id", PropKit.get("institution_id"));
  47. map.put("num", number.toString());
  48. String content = postWithParameters(GET_EXAM_URL, map);
  49. JSONObject jsonObject = JSON.parseObject(content);
  50. if (!jsonObject.getString("msg").equals("success")) {
  51. return null;
  52. }
  53. JSONArray jsonArray = JSON.parseArray(jsonObject.getString("data"));
  54. return jsonArray;
  55. }
  56. /**
  57. * 写入报告
  58. * @param instutionId
  59. * @param number
  60. */
  61. public void saveReport(Map <String,String> map) {
  62. postWithParameters(SAVE_REPORT_URL, map);
  63. }
  64. /**
  65. * 更新患者信息
  66. *
  67. * @param instutionId
  68. * @param number
  69. */
  70. public void updatePatientInfo(Map<String, String> map) {
  71. postWithParameters(UPDATE_PATIENT_URL, map);
  72. }
  73. /**
  74. * 创建登记信息
  75. *
  76. * @param instutionId
  77. * @param number
  78. */
  79. public String createRegisterInfo(Map<String, String> map) {
  80. String content = postWithParameters(CREATE_REGISTER_URL, map);
  81. JSONObject jsonObject = JSON.parseObject(content);
  82. if (!jsonObject.getString("msg").equals("success")) {
  83. return null;
  84. }
  85. String data = jsonObject.getString("data");
  86. return data;
  87. }
  88. /**
  89. * 保存附件
  90. *
  91. * @param instutionId
  92. * @param number
  93. * @throws FileNotFoundException
  94. */
  95. public void saveAnnex(Map<String, String> map, String filePath) {
  96. AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(PropKit.get("oss_endpoint"), PropKit.get("region"));
  97. BasicAWSCredentials credentials = new BasicAWSCredentials(PropKit.get("oss_accessKey"), PropKit.get("oss_secretKey"));
  98. AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
  99. AmazonS3 client = AmazonS3ClientBuilder.standard()
  100. .withEndpointConfiguration(endpointConfiguration)
  101. .withCredentials(credentialsProvider).build();
  102. String fileNameStr[] = filePath.split("=");
  103. String fileName = fileNameStr[fileNameStr.length - 2];
  104. String fileStorePath = getFileWithUrl(filePath, fileName);
  105. File file = new File(fileStorePath);
  106. String key = PropKit.get("institution_id") + "/" + fileName;
  107. boolean exists = client.doesObjectExist(PropKit.get("oss_bucketName"), key);
  108. if (!exists) {
  109. PutObjectRequest request = new PutObjectRequest(PropKit.get("oss_bucketName"), key, file);
  110. request.setCannedAcl(CannedAccessControlList.PublicRead);
  111. client.putObject(request);
  112. boolean bexists = client.doesObjectExist(PropKit.get("oss_bucketName"), key);
  113. if (!bexists) {
  114. PutObjectRequest request2 = new PutObjectRequest(PropKit.get("oss_bucketName"), key, file);
  115. request.setCannedAcl(CannedAccessControlList.PublicRead);
  116. client.putObject(request);
  117. }
  118. }
  119. client.shutdown();
  120. map.put("url", "https://annex.eos.jinan-4.cmecloud.cn/" + key);
  121. map.put("name", key);
  122. postWithParameters(SAVE_ANNEX_URL, map);
  123. file.delete();
  124. }
  125. public static String postWithParameters(String url, Map<String, String> map) {
  126. FormBody.Builder formbody = new FormBody.Builder();
  127. for (Map.Entry<String, String> entry : map.entrySet()) {
  128. formbody.add(entry.getKey(), entry.getValue());
  129. }
  130. RequestBody requestBody = formbody.build();
  131. Request request = new Request.Builder()
  132. .url(url)
  133. .post(requestBody)
  134. .build();
  135. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  136. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  137. String content = response.body().string();
  138. return content;
  139. } catch (IOException e) {
  140. // TODO Auto-generated catch block
  141. e.printStackTrace();
  142. return null;
  143. }
  144. }
  145. public static String getFileWithUrl(String url, String filename) {
  146. Request request = new Request.Builder().url(url).build();
  147. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  148. if (!response.isSuccessful())
  149. throw new IOException("Unexpected code " + response);
  150. InputStream inputStream = response.body().source().inputStream();
  151. // 本地文件夹目录(下载位置)
  152. String folder = PropKit.get("oss_localPath");
  153. // 下载文件保存位置
  154. String savepath = folder + "/" + filename;
  155. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savepath)));
  156. byte[] data = new byte[1024];
  157. int len;
  158. int available = inputStream.available();
  159. while ((len = inputStream.read(data)) != -1) {
  160. bufferedOutputStream.write(data, 0, len);
  161. }
  162. bufferedOutputStream.flush();
  163. bufferedOutputStream.close();
  164. inputStream.close();
  165. return savepath;
  166. } catch (IOException e) {
  167. // TODO Auto-generated catch block
  168. e.printStackTrace();
  169. return "";
  170. }
  171. }
  172. //ftp://FTPUser:ftpuser@188.188.2.1:6161/20240726/1AFB48FCF39241AA8DE61F85EE6D847F/Report/Report.jpg"
  173. public String downloadFtpFile(String remoteFileName, String fileName) {
  174. FTPClient ftpClient = new FTPClient();
  175. int reply;
  176. try {
  177. ftpClient.connect(PropKit.get("ftp_host"), PropKit.getInt("ftp_port"));
  178. ftpClient.login(PropKit.get("ftp_user"), PropKit.get("ftp_password"));
  179. reply = ftpClient.getReplyCode();
  180. if (!FTPReply.isPositiveCompletion(reply)) {
  181. ftpClient.disconnect();
  182. return null;
  183. }
  184. ftpClient.setControlEncoding("UTF-8");
  185. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  186. ftpClient.enterLocalPassiveMode();
  187. File localFile = new File("/home/zskk/FTP_FILE" + File.separatorChar + fileName);
  188. OutputStream os = new FileOutputStream(localFile);
  189. //ftp中文名需要iso-8859-1字符
  190. boolean flag2 = ftpClient.retrieveFile(new String(remoteFileName.getBytes("GBK"), "iso-8859-1"), os);
  191. if (!flag2) {
  192. System.out.println("没有找到" + remoteFileName + "---该文件");
  193. localFile.delete();
  194. } else {
  195. System.out.println("=================== save success");
  196. }
  197. os.close();
  198. ftpClient.logout();
  199. ftpClient.disconnect();
  200. return "/home/zskk/FTP_FILE" + File.separatorChar + fileName;
  201. } catch (Exception e) {
  202. e.printStackTrace();
  203. return null;
  204. }
  205. }
  206. public static void main(String[] args) {
  207. Map <String,String> map = new HashMap<String,String>();
  208. map.put("institution_id", "47600001");
  209. map.put("num", "10");
  210. postWithParameters(GET_EXAM_URL, map);
  211. }
  212. }