jiapeng vor 6 Jahren
Ursprung
Commit
79b5ab2f98

+ 37 - 84
src/main/java/com/ekexiu/project/storage/diacrisis/AiFormatTask.java

@ -1,20 +1,17 @@
1 1
package com.ekexiu.project.storage.diacrisis;
2 2

3
import java.awt.image.BufferedImage;
3
import java.io.ByteArrayOutputStream;
4 4
import java.io.File;
5
import java.io.IOException;
5
import java.io.InputStream;
6
import java.net.HttpURLConnection;
7
import java.net.URL;
6 8

7
import javax.imageio.ImageIO;
8
import javax.imageio.ImageReader;
9
import javax.imageio.ImageWriter;
10
import javax.imageio.event.IIOReadWarningListener;
11
import javax.imageio.event.IIOWriteWarningListener;
12 9

10
import org.jfw.util.ConstData;
11
import org.jfw.util.io.IoUtil;
13 12
import org.jfw.util.log.LogFactory;
14 13
import org.jfw.util.log.Logger;
15 14

16

17

18 15
public class AiFormatTask implements Runnable {
19 16
	private static Logger LOG = LogFactory.getLog(AiFormatTask.class);
20 17
	private String taskId;
@ -29,89 +26,45 @@ public class AiFormatTask implements Runnable {
29 26

30 27
	@Override
31 28
	public void run() {
32

29
		String errCode = "ERR0403";// 调用协议返回错误
33 30
		File file = new File(aiTaskService.getDstPath(), taskId);
34
		file = new File(file,"data_enhance");
31
		file = new File(file, "data_enhance");
35 32
		if (!file.exists()) {
36 33
			LOG.error("Path[" + file.getAbsolutePath() + "] not exists");
37 34
			aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
38 35
		} else {
39
			int begin, end;
40
			if (taskType == 1) {
41
				begin = 1;
42
				end = 7;
43
			} else if (taskType == 2) {
44
				begin = 7;
45
				end = 14;
46
			} else {
47
				LOG.error("err task type[" + taskType + "]");
48
				aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
49
				return;
50
			}
51
			for (int i = begin; i < end; ++i) {
52
				File src = new File(file, "IM_" + String.format("%04d.DICOM", i));
53
				if (!src.exists()) {
54
					LOG.error("Path[" + src.getAbsolutePath() + "] not exists");
55
					aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
56
					return;
57
				}
58
				File dst = new File(file, "ASD_" + String.format("%02d", i));
59
				if (dst.exists()) {
60
					LOG.error("Path[" + dst.getAbsolutePath() + "]  exists");
61
					aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
62
					return;
63
				}
64
				if (dst.mkdir()) {
65

66
//					try {
67
//						DicomReader reader = new DicomReader();
68
//						reader.addIIOReadWarningListener(new WarningListener());
69
//						reader.setInput(src);
70
//						int nf = reader.getNumImages(true);
71
//						if (nf <= 0) {
72
//							LOG.error("file [" + src.getAbsolutePath() + "] invalid");
73
//							aiTaskService.updateTaskState(taskId, 551, "ERR0402", null);
74
//							return;
75
//						}
76
//
77
//						for (int k = 0; k < nf; ++k) {
78
//							BufferedImage bi = reader.read(i);
79
//							if (bi == null) {
80
//								LOG.error("file [" + src.getAbsolutePath() + "] invalid");
81
//								aiTaskService.updateTaskState(taskId, 551, "ERR0402", null);
82
//								return;
83
//							}
84
//
85
//							File jpg = new File(dst, String.format("ASD_%02d_%03d.JPG", i, k));
86
//							if (jpg.exists()) {
87
//								LOG.error("file[" + jpg.getAbsolutePath() + "]  exists");
88
//								aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
89
//								return;
90
//							}
91
//							ImageIO.write(bi, "JPG", jpg);
92
//						}
93
//					} catch (Exception e) {
94
						aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
95
						return;
96
//					}
97

98
				} else {
99
					LOG.error("makdir [" + dst.getAbsolutePath() + "] error");
100
					aiTaskService.updateTaskState(taskId, 552, "ERR0403", null);
101
					return;
36
			try {
37
				URL u = new URL("http://127.0.0.1:5002/test?pi=" + file.getAbsolutePath());
38
				HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
39
				if (httpURLConnection.getResponseCode() == 200) {
40
					InputStream in = httpURLConnection.getInputStream();
41
					try {
42
						ByteArrayOutputStream out = new ByteArrayOutputStream();
43
						byte[] buf = new byte[128];
44
						int len = 0;
45
						while ((len = in.read(buf)) != -1) {
46
							if (len > 0) {
47
								out.write(buf, 0, len);
48
							}
49
						}
50
						buf = out.toByteArray();
51
						if (buf.length > 0) {
52
							String ret = new String(buf, ConstData.UTF8);
53
							ret = ret.trim();
54
							if (ret.equals("1")) {
55
								aiTaskService.updateTaskState(taskId, 599, null, new AiDiacrisisTask(taskId, taskType, aiTaskService));
56
								return;
57
							}
58
						}
59
					} catch (Exception e) {
60
					} finally {
61
						IoUtil.close(in);
62
					}
102 63
				}
64
			} catch (Exception ee) {
103 65
			}
66
			aiTaskService.updateTaskState(taskId, 552, errCode, null);
104 67
		}
105
		aiTaskService.updateTaskState(taskId, 599, null, new AiDiacrisisTask(taskId, taskType, aiTaskService));
106
	}
107
}
108

109
class WarningListener implements IIOReadWarningListener, IIOWriteWarningListener {
110
	public void warningOccurred(ImageReader source, String warning) {
111
		System.err.println("READ WARNING: " + warning);
112 68
	}
113 69

114
	public void warningOccurred(ImageWriter source, int idx, String warning) {
115
		System.err.println("WRITE WARNING (image " + idx + "): " + warning);
116
	}
117 70
}

+ 60 - 34
src/main/java/com/ekexiu/project/storage/diacrisis/AiStrengthenTask.java

@ -1,12 +1,11 @@
1 1
package com.ekexiu.project.storage.diacrisis;
2 2

3
import java.io.ByteArrayOutputStream;
4 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
5 6
import java.io.InputStream;
6
import java.net.HttpURLConnection;
7
import java.net.URL;
7
import java.io.OutputStream;
8 8

9
import org.jfw.util.ConstData;
10 9
import org.jfw.util.io.IoUtil;
11 10
import org.jfw.util.log.LogFactory;
12 11
import org.jfw.util.log.Logger;
@ -27,41 +26,68 @@ public class AiStrengthenTask implements Runnable {
27 26
	public void run() {
28 27
		String errCode = "ERR0309";// 调用异常
29 28
		File file = new File(aiTaskService.getDstPath(), taskId);
30
		file = new File(file, "data_desen");
31
		try {
32
			URL u = new URL("http://127.0.0.1:5003/enhance?pi=" + file.getAbsolutePath());
33
			HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
34
			if (httpURLConnection.getResponseCode() == 200) {
35
				InputStream in = httpURLConnection.getInputStream();
36
				try {
37
					ByteArrayOutputStream out = new ByteArrayOutputStream();
38
					byte[] buf = new byte[128];
39
					int len = 0;
40
					while ((len = in.read(buf)) != -1) {
41
						if (len > 0) {
42
							out.write(buf, 0, len);
29
		File dd = new File(file,"data_enhance");
30
		if(!dd.exists()){
31
			if(dd.mkdir()){
32
				file = new File(file, "data_desen");
33
				if(file.exists() && file.isDirectory() && file.list().length>0){
34
					try{
35
						for(File f:file.listFiles()){
36
							File cpdst = new File(dd,f.getName());
37
							 InputStream in = new FileInputStream(f);
38
							   try{
39
								  OutputStream os = new FileOutputStream(cpdst) ;
40
								  try{
41
									  IoUtil.copy(in, os, false,false);
42
								  }finally{
43
									  IoUtil.close(os); 
44
								  }
45
							   }finally{
46
								   IoUtil.close(in);
47
							   }
43 48
						}
49
						aiTaskService.updateTaskState(taskId, 499, null, new AiFormatTask(taskId, taskType, aiTaskService));
50
						return;
51
					} catch (Exception ee) {
44 52
					}
45
					buf = out.toByteArray();
46
					if (buf.length > 0) {
47
						String ret = new String(buf, ConstData.UTF8);
48
						ret = ret.trim();
49
						if (ret.equals("1")) {
50
							aiTaskService.updateTaskState(taskId, 499, null, new AiFormatTask(taskId, taskType, aiTaskService));
51
							return;
52
						}
53
					}
54
					errCode = "ERR0307";// 调用协议返回错误
55
				} catch (Exception e) {
56
					errCode = "ERR0307";// 调用协议传输异常
57
				} finally {
58
					IoUtil.close(in);
59 53
				}
60
			} else {
61
				errCode = "ERR0308";// 调用协议状态异常 http status != 200
62 54
			}
63
		} catch (Exception ee) {
64 55
		}
56
		
57
//		try {
58
//			URL u = new URL("http://127.0.0.1:5003/enhance?pi=" + file.getAbsolutePath());
59
//			HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
60
//			if (httpURLConnection.getResponseCode() == 200) {
61
//				InputStream in = httpURLConnection.getInputStream();
62
//				try {
63
//					ByteArrayOutputStream out = new ByteArrayOutputStream();
64
//					byte[] buf = new byte[128];
65
//					int len = 0;
66
//					while ((len = in.read(buf)) != -1) {
67
//						if (len > 0) {
68
//							out.write(buf, 0, len);
69
//						}
70
//					}
71
//					buf = out.toByteArray();
72
//					if (buf.length > 0) {
73
//						String ret = new String(buf, ConstData.UTF8);
74
//						ret = ret.trim();
75
//						if (ret.equals("1")) {
76
//							aiTaskService.updateTaskState(taskId, 499, null, new AiFormatTask(taskId, taskType, aiTaskService));
77
//							return;
78
//						}
79
//					}
80
//					errCode = "ERR0307";// 调用协议返回错误
81
//				} catch (Exception e) {
82
//					errCode = "ERR0307";// 调用协议传输异常
83
//				} finally {
84
//					IoUtil.close(in);
85
//				}
86
//			} else {
87
//				errCode = "ERR0308";// 调用协议状态异常 http status != 200
88
//			}
89
//		} catch (Exception ee) {
90
//		}
65 91
		aiTaskService.updateTaskState(taskId, 450, errCode, null);
66 92
	}
67 93
}

+ 195 - 1
src/main/java/com/ekexiu/project/storage/diacrisis/AiTaskService.java

@ -3,10 +3,13 @@ package com.ekexiu.project.storage.diacrisis;
3 3
import java.io.File;
4 4
import java.io.FileInputStream;
5 5
import java.io.FileOutputStream;
6
import java.io.IOException;
6 7
import java.io.InputStream;
7 8
import java.io.OutputStream;
8 9
import java.sql.Connection;
9 10
import java.sql.SQLException;
11
import java.util.LinkedList;
12
import java.util.List;
10 13
import java.util.concurrent.TimeUnit;
11 14

12 15
import org.jfw.apt.annotation.Autowrie;
@ -17,6 +20,7 @@ import org.jfw.apt.web.annotation.operate.Get;
17 20
import org.jfw.apt.web.annotation.operate.Post;
18 21
import org.jfw.apt.web.annotation.param.JdbcConn;
19 22
import org.jfw.apt.web.annotation.param.PathVar;
23
import org.jfw.apt.web.annotation.param.Upload;
20 24
import org.jfw.util.PageQueryResult;
21 25
import org.jfw.util.StringUtil;
22 26
import org.jfw.util.context.JfwAppContext;
@ -24,6 +28,8 @@ import org.jfw.util.io.IoUtil;
24 28
import org.jfw.util.jdbc.JdbcUtil;
25 29
import org.jfw.util.log.LogFactory;
26 30
import org.jfw.util.log.Logger;
31
import org.jfw.util.web.fileupload.Item;
32
import org.jfw.util.web.fileupload.UploadItemIterator;
27 33

28 34
import com.ekexiu.project.hdfs.HdfsService;
29 35
import com.ekexiu.project.storage.system.SessionUser;
@ -33,7 +39,7 @@ public class AiTaskService {
33 39

34 40
	private static Logger LOG = LogFactory.getLog(AiTaskService.class);
35 41

36
	private long delayTime = 2000;
42
	private long delayTime = 2;
37 43
	
38 44
	@Autowrie
39 45
	private HdfsService hdfsService;
@ -42,6 +48,7 @@ public class AiTaskService {
42 48
	private AiTaskDao aiTaskDao;
43 49
	private File srcPath = new File("/ekexiu/storage_asd/src");
44 50
	private File dstPath = new File("/ekexiu/storage_asd/dst");
51
	private File uploadPath = new File("/ekexiu/storage_asd/upload");
45 52

46 53
	
47 54
	public HdfsService getHdfsService() {
@ -52,6 +59,14 @@ public class AiTaskService {
52 59
		this.hdfsService = hdfsService;
53 60
	}
54 61

62
	public File getUploadPath() {
63
		return uploadPath;
64
	}
65

66
	public void setUploadPath(File uploadPath) {
67
		this.uploadPath = uploadPath;
68
	}
69

55 70
	public File getSrcPath() {
56 71
		return srcPath;
57 72
	}
@ -136,6 +151,55 @@ public class AiTaskService {
136 151
		}
137 152
	}
138 153
	
154
	
155
	
156
	@Path("/upload")
157
	@Post
158
	public List<UploadFile> upload(@Upload UploadItemIterator it) throws Exception {
159
		List<UploadFile> ret = new LinkedList<UploadFile>();
160
		try {
161
			while (it.hasNext()) {
162
				Item item = it.next();
163
				if (!item.isFormField()) {
164
					String name = normalizeFileName(item.getName());
165
					int index = name.lastIndexOf('.');
166
					String ext = index >= 0 ? name.substring(index + 1) : "";
167
					ext = ext.trim();
168
					long fileLen = 0;
169
					int len = 0;
170
					UploadFile uf = this.buildTargetFile(ext);
171
					uf.setName(name);
172
					File file = uf.getFn();
173
					ret.add(uf);
174
					byte[] buf = new byte[8092];
175
					OutputStream of = new FileOutputStream(file);
176
					try {
177
						InputStream in = item.getInputStream();
178
						try {
179
							while ((len = in.read(buf)) >= 0) {
180
								if (len > 0) {
181
									of.write(buf, 0, len);
182
									fileLen += len;
183
								}
184
							}
185
							uf.setSize(fileLen);
186
						} finally {
187
							in.close();
188
						}
189
					} finally {
190
						of.close();
191
					}
192
				}
193
			}
194
			return ret;
195
		} catch (Exception e) {
196
			this.remove(ret);
197
			throw e;
198
		} finally {
199
			if (it != null)
200
				it.clean();
201
		}
202
	}
139 203

140 204
   @Path("/add")
141 205
   @Get
@ -161,6 +225,61 @@ public class AiTaskService {
161 225
		return id;
162 226
	}
163 227
   
228
   
229
   
230
   @Path("/new")
231
   @Get
232
   @Post
233
	public String insert(@JdbcConn(true) Connection con, @LoginUser SessionUser user, final int taskType,String[] file) throws IOException, SQLException {
234
	   File path;
235
	   do{
236
		path = new File(this.dstPath,  StringUtil.buildUUID());
237
	   }while(path.exists());
238
	 	if(!path.mkdir()){
239
	 		throw new RuntimeException("mkdir["+path.getAbsolutePath()+"] error");	
240
	 	}
241
	   
242
	   for(String f:file){
243
		   int idx = f.indexOf('.');
244
		   if(idx!=16){
245
			   throw new IllegalArgumentException("invalid param file");
246
		   }
247
		   File taskSrc = new File(this.uploadPath,f.substring(0,16));
248
		   File taskDst = new File(path,f.substring(17));
249
		   InputStream in = new FileInputStream(taskSrc);
250
		   try{
251
			  OutputStream os = new FileOutputStream(taskDst) ;
252
			  try{
253
				  IoUtil.copy(in, os, false,false);
254
			  }finally{
255
				  IoUtil.close(os); 
256
			  }
257
		   }finally{
258
			   IoUtil.close(in);
259
		   }
260
	   }
261
	   
262
		final String id = path.getName();
263
		AiTask task = new AiTask();
264
		task.setDataNum(id);
265
		task.setTaskType(taskType);
266
		task.setState(100);
267
		task.setStateCode(null);
268
		task.setId(id);
269
		task.setUid(user.getId());
270
		aiTaskDao.insert(con, task);
271
		
272
		JfwAppContext.getScheduledExecutorService().schedule(new Runnable() {
273
			@Override
274
			public void run() {
275
				updateTaskState(id, 200, null, new AiTransferTask(id, taskType,AiTaskService.this));
276
			}
277
		},this.delayTime,TimeUnit.MILLISECONDS);
278
		return id;
279
	}
280
   
281
   
282
   
164 283
   private static final String EMPTY_STRING_ARRAY[] = new String[0];
165 284
   @Get
166 285
   @Path("/pq")
@ -184,4 +303,79 @@ public class AiTaskService {
184 303
   public AiTask query(@JdbcConn Connection con,String id)throws SQLException{
185 304
	   return aiTaskDao.query(con, id);
186 305
   }
306
   
307
   
308
   
309
   private void remove(List<UploadFile> list) {
310
       for (UploadFile file : list) {
311
           if (file.fn != null)
312
               file.fn.delete();
313
       }
314
   }
315

316
   private UploadFile buildTargetFile(String ext) {
317
       File file;
318
       do {
319
           String fn =StringUtil.buildUUID();
320
           file = new File(this.uploadPath, StringUtil.buildUUID());
321
       } while (file.exists());
322
       UploadFile uf = new UploadFile();
323
       uf.setFn(file);
324
       uf.setUri(file.getName());
325
       return uf;
326
   }
327

328
   private String normalizeFileName(String fn) {
329
       int index = fn.indexOf('\\');
330
       if (index >= 0) {
331
           fn = fn.substring(fn.lastIndexOf('\\') + 1);
332
       }
333
       index = fn.indexOf('/');
334
       if (index >= 0) {
335
           fn = fn.substring(fn.lastIndexOf('/') + 1);
336
       }
337
       if (fn.length() == 0)
338
           throw new RuntimeException("invalid filename in Multipart/data request");
339
       return fn;
340
   }
341

342
   public static class UploadFile {
343
       private String name;
344
       private String uri;
345
       private long size;
346
       private transient File fn;
347

348
       public File getFn() {
349
           return fn;
350
       }
351

352
       public void setFn(File fn) {
353
           this.fn = fn;
354
       }
355

356
       public String getName() {
357
           return name;
358
       }
359

360
       public void setName(String name) {
361
           this.name = name;
362
       }
363

364
       public String getUri() {
365
           return uri;
366
       }
367

368
       public void setUri(String uri) {
369
           this.uri = uri;
370
       }
371

372
       public long getSize() {
373
           return size;
374
       }
375

376
       public void setSize(long size) {
377
           this.size = size;
378
       }
379
   }
380

187 381
}

+ 24 - 25
src/main/java/com/ekexiu/project/storage/diacrisis/AiTransferTask.java

@ -20,30 +20,29 @@ public class AiTransferTask implements Runnable {
20 20
	@Override
21 21
	public void run() {
22 22

23
		File file = new File(aiTaskService.getDstPath(), taskId);
24
		if (file.exists()) {
25
			LOG.error("Path[" + file.getAbsolutePath() + "] exists");
26
			aiTaskService.updateTaskState(taskId, 250, "ERR0199", null);
27
		} else {
28
				file = new File(file,"data");
29
				if (file.mkdirs()) {
30
					for(String fn:aiTaskService.getSrcPath().list()){
31
						if(fn.length()>6 &&  fn.substring(fn.length()-6).equalsIgnoreCase(".DICOM")){
32
							File src = new File(aiTaskService.getSrcPath(), fn);
33
							File dst = new File(file, fn);
34
							if (!aiTaskService.copyFile(src, dst)) {
35
								LOG.error("copy File[" + src.getAbsolutePath() + "] error");
36
								aiTaskService.updateTaskState(taskId, 250, "ERR0199", null);
37
								return;
38
							}
39
						}
40
					}
41
					aiTaskService.updateTaskState(taskId, 299, null, new AiCleanTask(taskId, taskType, aiTaskService));
42
				} else {
43
					LOG.error("mkdir File[" + file.getAbsolutePath() + "] error");
44
					aiTaskService.updateTaskState(taskId, 250, "ERR0199", null);
45
				}
46
		}
23
		// File file = new File(aiTaskService.getDstPath(), taskId);
24
		// if (file.exists()) {
25
		// LOG.error("Path[" + file.getAbsolutePath() + "] exists");
26
		// aiTaskService.updateTaskState(taskId, 250, "ERR0199", null);
27
		// } else {
28
		// file = new File(file,"data");
29
		// if (file.mkdirs()) {
30
		// for(String fn:aiTaskService.getSrcPath().list()){
31
		// if(fn.length()>6 &&
32
		// fn.substring(fn.length()-6).equalsIgnoreCase(".DICOM")){
33
		// File src = new File(aiTaskService.getSrcPath(), fn);
34
		// File dst = new File(file, fn);
35
		// if (!aiTaskService.copyFile(src, dst)) {
36
		// LOG.error("copy File[" + src.getAbsolutePath() + "] error");
37
		// aiTaskService.updateTaskState(taskId, 250, "ERR0199", null);
38
		// return;
39
		// }
40
		// }
41
		// }
42
		aiTaskService.updateTaskState(taskId, 299, null, new AiCleanTask(taskId, taskType, aiTaskService));
43
		// } else {
44
		// LOG.error("mkdir File[" + file.getAbsolutePath() + "] error");
45
		// aiTaskService.updateTaskState(taskId, 250, "ERR0199", null);
46
		// }
47 47
	}
48

49 48
}