DataService.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.zskk.service;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import com.alibaba.fastjson.JSON;
  12. import com.alibaba.fastjson.JSONArray;
  13. import com.alibaba.fastjson.JSONObject;
  14. import com.jfinal.kit.PropKit;
  15. import jcifs.smb.NtlmPasswordAuthentication;
  16. import jcifs.smb.SmbFile;
  17. import jcifs.smb.SmbFileInputStream;
  18. import okhttp3.FormBody;
  19. import okhttp3.MediaType;
  20. import okhttp3.OkHttpClient;
  21. import okhttp3.Request;
  22. import okhttp3.RequestBody;
  23. import okhttp3.Response;
  24. public class DataService {
  25. private static String GET_EXAM_URL = "https://risserver3.pacsonline.cn/butt/getExam";
  26. private static String SAVE_REPORT_URL = "https://risserver3.pacsonline.cn/butt/saveReport";
  27. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  28. public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");
  29. /**
  30. * 获取未出报告的检查列表
  31. * @param instutionId
  32. * @param number
  33. */
  34. public JSONArray getExamList(Integer number) {
  35. Map <String,String> map = new HashMap<String,String>();
  36. map.put("institution_id", PropKit.get("institution_id"));
  37. map.put("num", number.toString());
  38. String content = postWithParameters(GET_EXAM_URL, map);
  39. JSONObject jsonObject = JSON.parseObject(content);
  40. if (!jsonObject.getString("msg").equals("success")) {
  41. return null;
  42. }
  43. JSONArray jsonArray = JSON.parseArray(jsonObject.getString("data"));
  44. return jsonArray;
  45. }
  46. /**
  47. * 写入报告
  48. * @param instutionId
  49. * @param number
  50. */
  51. public void saveReport(Map <String,String> map) {
  52. postWithParameters(SAVE_REPORT_URL, map);
  53. }
  54. public static String postWithParameters(String url, Map<String, String> map) {
  55. FormBody.Builder formbody = new FormBody.Builder();
  56. for (Map.Entry<String, String> entry : map.entrySet()) {
  57. formbody.add(entry.getKey(), entry.getValue());
  58. }
  59. RequestBody requestBody = formbody.build();
  60. Request request = new Request.Builder()
  61. .url(url)
  62. .post(requestBody)
  63. .build();
  64. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  65. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  66. String content = response.body().string();
  67. return content;
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. return null;
  72. }
  73. }
  74. public static void downloadFileToFolder(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
  75. InputStream in = null;
  76. OutputStream out = null;
  77. try {
  78. SmbFile remoteFile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
  79. File localFile = new File(localDir + File.separator + fileName);
  80. localFile.getParentFile().mkdirs();
  81. in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
  82. out = new BufferedOutputStream(new FileOutputStream(localFile));
  83. byte[] buffer = new byte[1024];
  84. while (in.read(buffer) != -1) {
  85. out.write(buffer);
  86. buffer = new byte[1024];
  87. }
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. } finally {
  91. try {
  92. out.close();
  93. in.close();
  94. System.out.print("11");
  95. File localFile = new File(localDir + File.separator + fileName);
  96. Request request = new Request.Builder()
  97. .url("https://api.github.com/markdown/raw")
  98. .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, localFile))
  99. .build();
  100. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  101. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  102. System.out.println(response.body().string());
  103. }
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }
  109. public static void main(String[] args) {
  110. // Map <String,String> map = new HashMap<String,String>();
  111. // map.put("institution_id", "47600001");
  112. // map.put("num", "10");
  113. // postWithParameters(GET_EXAM_URL, map);
  114. downloadFileToFolder("smb://"+"hao:"+"123456@" +"192.168.31.150", "/share/pagkage", "factor1.py", "./tempImg");
  115. }
  116. }