123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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();
- Integer ran1 = r.nextInt(10000);
- 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;
- }
- }
|