功能是把文件上传成功后删除
同步
try { logger.debug("begin seed File {}", DateKit.toStr(new Date(), DateKit.timeStampPattern)); Map<String, Object> headers = new HashMap<>(16); headers.put(CommonAttribute.BINFILE_MD5, UtilMd5.encoderByMd5(file.toFile())); headers.put(CommonAttribute.APPFLAG, appFlag); RawResponse rawResponse = HttpClientPlugin.getRequestBuilder().headers(headers) .multiPartBody(Part.file(CommonAttribute.BINFILE, file.toFile())).send(); if (rawResponse.statusCode() == 200) { logger.info("Success send file {} to server !", file.getFileName()); Files.deleteIfExists(file); //删除文件 logger.info("Delete file {}", file.getFileName()); } } catch (Throwable e) { logger.info("Send file to server is fail!"); logger.error(e.getMessage(), e); }
异步
DeferredManager dm = new DefaultDeferredManager(); dm.when(() -> { asyncClientSize.incrementAndGet(); logger.debug("begin seed File {}", DateKit.toStr(new Date(), DateKit.timeStampPattern)); Map<String, Object> headers = new HashMap<>(16); headers.put(CommonAttribute.BINFILE_MD5, UtilMd5.encoderByMd5(file.toFile())); headers.put(CommonAttribute.APPFLAG, appFlag); return HttpClientPlugin.getRequestBuilder().headers(headers) .multiPartBody(Part.file(CommonAttribute.BINFILE, file.toFile())).send(); }).done(rawResponse -> { if (rawResponse.statusCode() == 200) { logger.info("Success send file {} to server !", file.getFileName()); UtilFiles.deleteFile(file); //删除文件 logger.info("Delete file {}", file.getFileName()); } }).fail(e -> { logger.error("Send file to server is fail!"); logger.error(e.getMessage(), e); }).always((state,rawResponse,e)->asyncClientSize.decrementAndGet());
pom.xml
<dependency> <groupId>net.dongliu</groupId> <artifactId>requests</artifactId> </dependency> <dependency> <groupId>org.jdeferred.v2</groupId> <artifactId>jdeferred-core</artifactId> </dependency>