DownloadTask.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.zskk.task;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Random;
  10. import com.jfinal.plugin.activerecord.Db;
  11. import com.jfinal.plugin.activerecord.Record;
  12. import com.jfinal.plugin.cron4j.ITask;
  13. import com.zskk.service.ServiceFactory;
  14. import com.zskk.service.ThreadPoolService;
  15. import com.zskk.tools.ExecUtil;
  16. import okhttp3.Call;
  17. import okhttp3.Callback;
  18. import okhttp3.OkHttpClient;
  19. import okhttp3.Request;
  20. import okhttp3.Response;
  21. import okhttp3.ResponseBody;
  22. import oracle.net.aso.i;
  23. public class DownloadTask implements ITask {
  24. @Override
  25. public void run() {
  26. // TODO Auto-generated method stub
  27. OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  28. Integer max = 2;
  29. List<Record> count = Db.use("local").find("select * from study where status =4");
  30. //同时下载的队列
  31. Integer flag = count.size();
  32. if (flag >= max) {
  33. return;
  34. }
  35. List<Record> studyidfinds = Db.use("local").find("select * from study where status =1 or status =2 order by createAt asc limit 5");
  36. if (studyidfinds == null) {
  37. return;
  38. }
  39. for (Record studyidfind : studyidfinds) {
  40. flag++;
  41. if (flag > max) {
  42. return;
  43. }
  44. //状态4:下载中
  45. studyidfind.set("status", 4);
  46. Db.use("local").update("study", studyidfind);
  47. List<Record> dicomfind = Db.use("connected_dicom").find("select * from dicominfo where studyuid=?",studyidfind.getStr("studyuid"));
  48. for (Record record : dicomfind) {
  49. Request request = new Request.Builder()
  50. .url(record.getStr("URL"))
  51. .build();
  52. OKHTTP_CLIENT.newCall(request).enqueue(new Callback() {
  53. @Override public void onFailure(Call call, IOException e) {
  54. e.printStackTrace();
  55. }
  56. @Override public void onResponse(Call call, Response response) throws IOException {
  57. try (ResponseBody responseBody = response.body()) {
  58. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  59. writeFile(response,studyidfind.getStr("studyuid"));
  60. }
  61. }
  62. });
  63. }
  64. studyidfind.set("status", 3);
  65. Db.use("local").update("study", studyidfind);
  66. }
  67. }
  68. @Override
  69. public void stop() {
  70. // TODO Auto-generated method stub
  71. }
  72. private void writeFile(Response response, String uid) {
  73. InputStream is = null;
  74. FileOutputStream fos = null;
  75. is = response.body().byteStream();
  76. String path = "/home/lenovo/CFIND_XML";
  77. Random r = new Random();
  78. Integer ran1 = r.nextInt(10000);
  79. File file = new File(path, uid+ran1.toString()+".dcm");
  80. try {
  81. fos = new FileOutputStream(file);
  82. byte[] bytes = new byte[1024];
  83. int len = 0;
  84. //获取下载的文件的大小
  85. long fileSize = response.body().contentLength();
  86. long sum = 0;
  87. int porSize = 0;
  88. while ((len = is.read(bytes)) != -1) {
  89. fos.write(bytes);
  90. sum += len;
  91. porSize = (int) ((sum * 1.0f / fileSize) * 100);
  92. }
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. } finally {
  96. try {
  97. if (is != null) {
  98. is.close();
  99. }
  100. if (fos != null) {
  101. fos.close();
  102. }
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. }
  108. /**
  109. * 日期字符串格式转换
  110. *
  111. * @param dateStr
  112. * @return
  113. */
  114. private String parseStringToDate() {
  115. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  116. Date date = new Date();
  117. long dInteger = date.getTime() - 600000;
  118. String daString = sdf.format(new Date(dInteger));
  119. return daString;
  120. }
  121. }