jiapeng 5 anos atrás
pai
commit
a3e23ab9b4

+ 298 - 160
src/main/java/com/ekexiu/project/storage/diacrisis/AiTaskService.java

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

3
import java.io.ByteArrayOutputStream;
3 4
import java.io.File;
4 5
import java.io.FileInputStream;
5 6
import java.io.FileOutputStream;
6 7
import java.io.IOException;
7 8
import java.io.InputStream;
8 9
import java.io.OutputStream;
10
import java.net.HttpURLConnection;
11
import java.net.URL;
12
import java.net.URLEncoder;
9 13
import java.sql.Connection;
10 14
import java.sql.SQLException;
15
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.Iterator;
11 18
import java.util.LinkedList;
12 19
import java.util.List;
13 20
import java.util.concurrent.TimeUnit;
@ -21,11 +28,15 @@ import org.jfw.apt.web.annotation.operate.Post;
21 28
import org.jfw.apt.web.annotation.param.JdbcConn;
22 29
import org.jfw.apt.web.annotation.param.PathVar;
23 30
import org.jfw.apt.web.annotation.param.Upload;
31
import org.jfw.util.ArrayUtil;
32
import org.jfw.util.ConstData;
33
import org.jfw.util.ListUtil;
24 34
import org.jfw.util.PageQueryResult;
25 35
import org.jfw.util.StringUtil;
26 36
import org.jfw.util.context.JfwAppContext;
27 37
import org.jfw.util.io.IoUtil;
28 38
import org.jfw.util.jdbc.JdbcUtil;
39
import org.jfw.util.json.JsonService;
29 40
import org.jfw.util.log.LogFactory;
30 41
import org.jfw.util.log.Logger;
31 42
import org.jfw.util.web.fileupload.Item;
@ -33,6 +44,7 @@ import org.jfw.util.web.fileupload.UploadItemIterator;
33 44

34 45
import com.ekexiu.project.hdfs.HdfsService;
35 46
import com.ekexiu.project.storage.system.SessionUser;
47
import com.google.gson.Gson;
36 48

37 49
@Path("/task")
38 50
public class AiTaskService {
@ -40,7 +52,7 @@ public class AiTaskService {
40 52
	private static Logger LOG = LogFactory.getLog(AiTaskService.class);
41 53

42 54
	private long delayTime = 2;
43
	
55

44 56
	@Autowrie
45 57
	private HdfsService hdfsService;
46 58

@ -50,7 +62,6 @@ public class AiTaskService {
50 62
	private File dstPath = new File("/ekexiu/storage_asd/dst");
51 63
	private File uploadPath = new File("/ekexiu/storage_asd/upload");
52 64

53
	
54 65
	public HdfsService getHdfsService() {
55 66
		return hdfsService;
56 67
	}
@ -91,17 +102,18 @@ public class AiTaskService {
91 102
		this.aiTaskDao = aiTaskDao;
92 103
	}
93 104

94
	public void uploadToHdfs(final String dst,final File src){
105
	public void uploadToHdfs(final String dst, final File src) {
95 106
		JfwAppContext.getScheduledExecutorService().submit(new Runnable() {
96 107
			@Override
97 108
			public void run() {
98
			    try{
99
			    	hdfsService.upload(dst, src);
100
			    }catch(Throwable tr){
101
			    }
109
				try {
110
					hdfsService.upload(dst, src);
111
				} catch (Throwable tr) {
112
				}
102 113
			}
103 114
		});
104 115
	}
116

105 117
	public boolean copyFile(File src, File dst) {
106 118
		try {
107 119
			if (src.exists() && (!dst.exists())) {
@ -150,9 +162,7 @@ public class AiTaskService {
150 162
		} catch (Exception e1) {
151 163
		}
152 164
	}
153
	
154
	
155
	
165

156 166
	@Path("/upload")
157 167
	@Post
158 168
	public List<UploadFile> upload(@Upload UploadItemIterator it) throws Exception {
@ -201,9 +211,9 @@ public class AiTaskService {
201 211
		}
202 212
	}
203 213

204
   @Path("/add")
205
   @Get
206
   @Post
214
	@Path("/add")
215
	@Get
216
	@Post
207 217
	public String insert(@JdbcConn(true) Connection con, @LoginUser SessionUser user, final String dataNum, final int taskType) throws SQLException {
208 218
		final String id = StringUtil.buildUUID();
209 219
		AiTask task = new AiTask();
@ -215,56 +225,54 @@ public class AiTaskService {
215 225
		task.setId(id);
216 226
		task.setUid(user.getId());
217 227
		aiTaskDao.insert(con, task);
218
		
228

219 229
		JfwAppContext.getScheduledExecutorService().schedule(new Runnable() {
220 230
			@Override
221 231
			public void run() {
222
				updateTaskState(id, 200, null, new AiTransferTask(id, taskType,AiTaskService.this));
232
				updateTaskState(id, 200, null, new AiTransferTask(id, taskType, AiTaskService.this));
223 233
			}
224
		},this.delayTime,TimeUnit.MILLISECONDS);
234
		}, this.delayTime, TimeUnit.MILLISECONDS);
225 235
		return id;
226 236
	}
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
	   String taskId;
236
	   do{
237
		path = new File(this.dstPath,  StringUtil.buildUUID());
238
	   }while(path.exists());
239
	 	if(!path.mkdir()){
240
	 		throw new RuntimeException("mkdir["+path.getAbsolutePath()+"] error");	
241
	 	}
242
	 	taskId = path.getName();
243
	 	path = new File(path,"data");
244
	 	if(!path.mkdir()){
245
	 		throw new RuntimeException("mkdir["+path.getAbsolutePath()+"] error");	
246
	 	}
247
	   
248
	   for(String f:file){
249
		   int idx = f.indexOf('.');
250
		   if(idx!=32){
251
			   throw new IllegalArgumentException("invalid param file");
252
		   }
253
		   File taskSrc = new File(this.uploadPath,f.substring(0,32));
254
		   File taskDst = new File(path,f.substring(33));
255
		   InputStream in = new FileInputStream(taskSrc);
256
		   try{
257
			  OutputStream os = new FileOutputStream(taskDst) ;
258
			  try{
259
				  IoUtil.copy(in, os, false,false);
260
			  }finally{
261
				  IoUtil.close(os); 
262
			  }
263
		   }finally{
264
			   IoUtil.close(in);
265
		   }
266
	   }
267
	   
237

238
	@Path("/new")
239
	@Get
240
	@Post
241
	public String insert(@JdbcConn(true) Connection con, @LoginUser SessionUser user, final int taskType, String[] file) throws IOException, SQLException {
242
		File path;
243
		String taskId;
244
		do {
245
			path = new File(this.dstPath, StringUtil.buildUUID());
246
		} while (path.exists());
247
		if (!path.mkdir()) {
248
			throw new RuntimeException("mkdir[" + path.getAbsolutePath() + "] error");
249
		}
250
		taskId = path.getName();
251
		path = new File(path, "data");
252
		if (!path.mkdir()) {
253
			throw new RuntimeException("mkdir[" + path.getAbsolutePath() + "] error");
254
		}
255

256
		for (String f : file) {
257
			int idx = f.indexOf('.');
258
			if (idx != 32) {
259
				throw new IllegalArgumentException("invalid param file");
260
			}
261
			File taskSrc = new File(this.uploadPath, f.substring(0, 32));
262
			File taskDst = new File(path, f.substring(33));
263
			InputStream in = new FileInputStream(taskSrc);
264
			try {
265
				OutputStream os = new FileOutputStream(taskDst);
266
				try {
267
					IoUtil.copy(in, os, false, false);
268
				} finally {
269
					IoUtil.close(os);
270
				}
271
			} finally {
272
				IoUtil.close(in);
273
			}
274
		}
275

268 276
		final String id = taskId;
269 277
		AiTask task = new AiTask();
270 278
		task.setDataNum(id.substring(24));
@ -274,114 +282,244 @@ public class AiTaskService {
274 282
		task.setId(id);
275 283
		task.setUid(user.getId());
276 284
		aiTaskDao.insert(con, task);
277
		
285

278 286
		JfwAppContext.getScheduledExecutorService().schedule(new Runnable() {
279 287
			@Override
280 288
			public void run() {
281
				updateTaskState(id, 200, null, new AiTransferTask(id, taskType,AiTaskService.this));
289
				updateTaskState(id, 200, null, new AiTransferTask(id, taskType, AiTaskService.this));
282 290
			}
283
		},this.delayTime,TimeUnit.MILLISECONDS);
291
		}, this.delayTime, TimeUnit.MILLISECONDS);
284 292
		return id;
285 293
	}
286
   
287
   
288
   
289
   private static final String EMPTY_STRING_ARRAY[] = new String[0];
290
   @Get
291
   @Path("/pq")
292
   public PageQueryResult<AiTask> pageQuery(@JdbcConn Connection con,@LoginUser SessionUser user,@Nullable String dataNum,@Nullable Integer taskType,int pageSize,int pageNo) throws SQLException{
293
	   return aiTaskDao.pageQuery(con, user.getId(), dataNum, taskType, pageSize, pageNo);
294
   }
295
   @Get
296
   @Path("/result/{taskid}")
297
   public String[] getResult(@PathVar String taskid){
298
	   File path = new File(dstPath,taskid);
299
	   if(path.exists() && path.isDirectory()){
300
		   path = new File(path,"RESULT");
301
		   if(path.exists() && path.isDirectory()){
302
			   return path.list();
303
		   }
304
	   }
305
	   return EMPTY_STRING_ARRAY;
306
   }
307
   @Get
308
   @Path("/qo")
309
   public AiTask query(@JdbcConn Connection con,String id)throws SQLException{
310
	   return aiTaskDao.query(con, id);
311
   }
312
   
313
   
314
   
315
   private void remove(List<UploadFile> list) {
316
       for (UploadFile file : list) {
317
           if (file.fn != null)
318
               file.fn.delete();
319
       }
320
   }
321

322
   private UploadFile buildTargetFile(String ext) {
323
       File file;
324
       do {
325
           String fn =StringUtil.buildUUID();
326
           file = new File(this.uploadPath, StringUtil.buildUUID());
327
       } while (file.exists());
328
       UploadFile uf = new UploadFile();
329
       uf.setFn(file);
330
       uf.setUri(file.getName());
331
       return uf;
332
   }
333

334
   private String normalizeFileName(String fn) {
335
       int index = fn.indexOf('\\');
336
       if (index >= 0) {
337
           fn = fn.substring(fn.lastIndexOf('\\') + 1);
338
       }
339
       index = fn.indexOf('/');
340
       if (index >= 0) {
341
           fn = fn.substring(fn.lastIndexOf('/') + 1);
342
       }
343
       if (fn.length() == 0)
344
           throw new RuntimeException("invalid filename in Multipart/data request");
345
       return fn;
346
   }
347

348
   public static class UploadFile {
349
       private String name;
350
       private String uri;
351
       private long size;
352
       private transient File fn;
353

354
       public File getFn() {
355
           return fn;
356
       }
357

358
       public void setFn(File fn) {
359
           this.fn = fn;
360
       }
361

362
       public String getName() {
363
           return name;
364
       }
365

366
       public void setName(String name) {
367
           this.name = name;
368
       }
369

370
       public String getUri() {
371
           return uri;
372
       }
373

374
       public void setUri(String uri) {
375
           this.uri = uri;
376
       }
377

378
       public long getSize() {
379
           return size;
380
       }
381

382
       public void setSize(long size) {
383
           this.size = size;
384
       }
385
   }
386 294

295
	private static final String EMPTY_STRING_ARRAY[] = new String[0];
296

297
	@Get
298
	@Path("/pq")
299
	public PageQueryResult<AiTask> pageQuery(@JdbcConn Connection con, @LoginUser SessionUser user, @Nullable String dataNum, @Nullable Integer taskType,
300
			int pageSize, int pageNo) throws SQLException {
301
		return aiTaskDao.pageQuery(con, user.getId(), dataNum, taskType, pageSize, pageNo);
302
	}
303

304
	@Get
305
	@Path("/result/{taskid}"")
306
	public String[] getResult(@PathVar String taskid) {
307
		File path = new File(dstPath, taskid);
308
		if (path.exists() && path.isDirectory()) {
309
			path = new File(path, "RESULT");
310
			if (path.exists() && path.isDirectory()) {
311
				return path.list();
312
			}
313
		}
314
		return EMPTY_STRING_ARRAY;
315
	}
316

317
	@Get
318
	@Path("/qo")
319
	public AiTask query(@JdbcConn Connection con, String id) throws SQLException {
320
		return aiTaskDao.query(con, id);
321
	}
322

323
	private static final String[] EMTPY_STRING_ARRAY = new String[0]; 
324
	private static final Gson gson = new Gson();
325
	@Get
326
	@Path("/searchJpg")
327
	public List<String> serch(String id) throws IOException {
328
		List<String> sret = new LinkedList<String>();
329
		URL u = new URL("http://127.0.0.1:5005/test?pi=" + "/ekexiu/storage_asd/dst/" + id + "/data_trans");
330
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
331
		if (httpURLConnection.getResponseCode() == 200) {
332
			InputStream in = httpURLConnection.getInputStream();
333
			try {
334
				ByteArrayOutputStream out = new ByteArrayOutputStream();
335
				byte[] buf = new byte[128];
336
				int len = 0;
337
				while ((len = in.read(buf)) != -1) {
338
					if (len > 0) {
339
						out.write(buf, 0, len);
340
					}
341
				}
342
				buf = out.toByteArray();
343
				if (buf.length > 0) {
344
					String ret = new String(buf, ConstData.UTF8);
345
					ret = ret.trim();
346
					if(ret.equals("0")){
347
						return Collections.<String>emptyList();
348
					}
349
					if(ret.startsWith("[") && ret.endsWith("]")){
350
						Object[] os=JsonService.fromJson(ret,Object[].class);
351
						for(Object o:os){
352
							if(o instanceof Collection<?>){
353
								Collection<?> ll =(Collection<?>)o;
354
								if(ll.size()==3){
355
									Iterator<?> it = ll.iterator();
356
									it.hasNext();
357
									it.next();
358
									it.hasNext();
359
									it.next();
360
									if(it.hasNext()){
361
										Object so = it.next();
362
										if(so instanceof String){
363
											sret.add((String)so);
364
										}
365
									}
366
								}
367
							}
368
						}
369
						return sret;
370
					}
371

372
				}
373

374
			} finally {
375
				IoUtil.close(in);
376
			}
377
		}
378
		throw new IOException("call 127.0.0.1 error");
379
	}
380

381
	
382
	@Get
383
	@Path("/searchTxt")
384
	public List<String> serchTxt(String txt) throws IOException {
385
		List<String> sret = new LinkedList<String>();
386
		URL u = new URL("http://127.0.0.1:5004/test?pi=" +URLEncoder.encode(txt,"UTF-8"));
387
		HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
388
		if (httpURLConnection.getResponseCode() == 200) {
389
			InputStream in = httpURLConnection.getInputStream();
390
			try {
391
				ByteArrayOutputStream out = new ByteArrayOutputStream();
392
				byte[] buf = new byte[128];
393
				int len = 0;
394
				while ((len = in.read(buf)) != -1) {
395
					if (len > 0) {
396
						out.write(buf, 0, len);
397
					}
398
				}
399
				buf = out.toByteArray();
400
				if (buf.length > 0) {
401
					String ret = new String(buf, ConstData.UTF8);
402
					ret = ret.trim();
403
					if(ret.equals("0")){
404
						return Collections.<String>emptyList();
405
					}
406
					if(ret.startsWith("[") && ret.endsWith("]")){
407
						Object[] os=JsonService.fromJson(ret,Object[].class);
408
						for(Object o:os){
409
							if(o instanceof Collection<?>){
410
								Collection<?> ll =(Collection<?>)o;
411
								if(ll.size()==3){
412
									Iterator<?> it = ll.iterator();
413
									it.hasNext();
414
									it.next();
415
									it.hasNext();
416
									it.next();
417
									if(it.hasNext()){
418
										Object so = it.next();
419
										if(so instanceof String){
420
											sret.add((String)so);
421
										}
422
									}
423
								}
424
							}
425
						}
426
						return sret;
427
					}
428

429
				}
430

431
			} finally {
432
				IoUtil.close(in);
433
			}
434
		}
435
		throw new IOException("call 127.0.0.1 error");
436
	}
437
	private void remove(List<UploadFile> list) {
438
		for (UploadFile file : list) {
439
			if (file.fn != null)
440
				file.fn.delete();
441
		}
442
	}
443

444
	private UploadFile buildTargetFile(String ext) {
445
		File file;
446
		do {
447
			String fn = StringUtil.buildUUID();
448
			file = new File(this.uploadPath, StringUtil.buildUUID());
449
		} while (file.exists());
450
		UploadFile uf = new UploadFile();
451
		uf.setFn(file);
452
		uf.setUri(file.getName());
453
		return uf;
454
	}
455

456
	private String normalizeFileName(String fn) {
457
		int index = fn.indexOf('\\');
458
		if (index >= 0) {
459
			fn = fn.substring(fn.lastIndexOf('\\') + 1);
460
		}
461
		index = fn.indexOf('/');
462
		if (index >= 0) {
463
			fn = fn.substring(fn.lastIndexOf('/') + 1);
464
		}
465
		if (fn.length() == 0)
466
			throw new RuntimeException("invalid filename in Multipart/data request");
467
		return fn;
468
	}
469

470
	public static class UploadFile {
471
		private String name;
472
		private String uri;
473
		private long size;
474
		private transient File fn;
475

476
		public File getFn() {
477
			return fn;
478
		}
479

480
		public void setFn(File fn) {
481
			this.fn = fn;
482
		}
483

484
		public String getName() {
485
			return name;
486
		}
487

488
		public void setName(String name) {
489
			this.name = name;
490
		}
491

492
		public String getUri() {
493
			return uri;
494
		}
495

496
		public void setUri(String uri) {
497
			this.uri = uri;
498
		}
499

500
		public long getSize() {
501
			return size;
502
		}
503

504
		public void setSize(long size) {
505
			this.size = size;
506
		}
507
	}
508

509
	
510
	public static void main(String[] args){
511
		String s ="[\"aa\",[\"0\",1,\"0\"],[\"0\",0]]";
512
		
513
		Object[] os=JsonService.fromJson(s,Object[].class);
514
		for(Object o:os){
515
			System.out.println(o.getClass().getName());
516
			if(o instanceof Collection<?>){
517
				for(Object oo:(Collection<?>)o){
518
					System.out.println("========"+oo.getClass().getName());
519
				}
520
			}
521
		}
522
		
523
		
524
	}
387 525
}