|
@@ -0,0 +1,136 @@
|
|
|
+package com.zskk.task;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+import com.jfinal.plugin.activerecord.Db;
|
|
|
+import com.jfinal.plugin.activerecord.Record;
|
|
|
+import com.jfinal.plugin.cron4j.ITask;
|
|
|
+import com.zskk.service.ServiceFactory;
|
|
|
+import com.zskk.service.ThreadPoolService;
|
|
|
+import com.zskk.tools.ExecUtil;
|
|
|
+
|
|
|
+import okhttp3.Call;
|
|
|
+import okhttp3.Callback;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
+import okhttp3.ResponseBody;
|
|
|
+import oracle.net.aso.i;
|
|
|
+
|
|
|
+public class DownloadTask implements ITask {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
|
|
|
+
|
|
|
+ Integer max = 2;
|
|
|
+ List<Record> count = Db.use("local").find("select * from study where status =4");
|
|
|
+ //同时下载的队列
|
|
|
+ Integer flag = count.size();
|
|
|
+ if (flag >= max) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Record> studyidfinds = Db.use("local").find("select * from study where status =1 or status =2 order by createAt asc limit 5");
|
|
|
+ if (studyidfinds == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (Record studyidfind : studyidfinds) {
|
|
|
+ flag++;
|
|
|
+ if (flag > max) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //状态4:下载中
|
|
|
+ studyidfind.set("status", 4);
|
|
|
+ Db.use("local").update("study", studyidfind);
|
|
|
+ List<Record> dicomfind = Db.use("connected_dicom").find("select * from dicominfo where studyuid=?",studyidfind.getStr("studyuid"));
|
|
|
+ for (Record record : dicomfind) {
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(record.getStr("URL"))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ OKHTTP_CLIENT.newCall(request).enqueue(new Callback() {
|
|
|
+ @Override public void onFailure(Call call, IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override public void onResponse(Call call, Response response) throws IOException {
|
|
|
+ try (ResponseBody responseBody = response.body()) {
|
|
|
+ if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
|
|
+ writeFile(response,studyidfind.getStr("studyuid"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ studyidfind.set("status", 3);
|
|
|
+ Db.use("local").update("study", studyidfind);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void stop() {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void writeFile(Response response, String uid) {
|
|
|
+ InputStream is = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ is = response.body().byteStream();
|
|
|
+ String path = "/home/lenovo/CFIND_XML";
|
|
|
+ Random r = new Random(1);
|
|
|
+ Integer ran1 = r.nextInt(1000);
|
|
|
+ File file = new File(path, uid+ran1.toString()+".dcm");
|
|
|
+ try {
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ //获取下载的文件的大小
|
|
|
+ long fileSize = response.body().contentLength();
|
|
|
+ long sum = 0;
|
|
|
+ int porSize = 0;
|
|
|
+ while ((len = is.read(bytes)) != -1) {
|
|
|
+ fos.write(bytes);
|
|
|
+ sum += len;
|
|
|
+ porSize = (int) ((sum * 1.0f / fileSize) * 100);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (fos != null) {
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日期字符串格式转换
|
|
|
+ *
|
|
|
+ * @param dateStr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String parseStringToDate() {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = new Date();
|
|
|
+ long dInteger = date.getTime() - 600000;
|
|
|
+ String daString = sdf.format(new Date(dInteger));
|
|
|
+ return daString;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|