From 2161d3ca1d56d3046d1fd52219c54ed353a701e0 Mon Sep 17 00:00:00 2001 From: yixian Date: Thu, 16 Jul 2020 03:01:56 +1000 Subject: [PATCH] reset ato output --- .../core/impls/ATOReportServiceImpl.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/analysis/core/impls/ATOReportServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/analysis/core/impls/ATOReportServiceImpl.java index 4279c0277..f2f724059 100644 --- a/src/main/java/au/com/royalpay/payment/manage/analysis/core/impls/ATOReportServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/analysis/core/impls/ATOReportServiceImpl.java @@ -4,7 +4,6 @@ import au.com.royalpay.payment.manage.analysis.beans.ato.*; import au.com.royalpay.payment.manage.analysis.core.ATOReportService; import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper; import au.com.royalpay.payment.manage.mappers.system.ClientMapper; -import au.com.royalpay.payment.tools.connections.attachment.core.AttachmentClient; import au.com.royalpay.payment.tools.env.PlatformEnvironment; import au.com.royalpay.payment.tools.exceptions.BadRequestException; import au.com.royalpay.payment.tools.exceptions.ServerErrorException; @@ -12,8 +11,9 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.codec.Charsets; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.joda.time.DateTime; import org.slf4j.Logger; @@ -28,9 +28,7 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.Date; @@ -54,13 +52,11 @@ public class ATOReportServiceImpl implements ATOReportService { private Map industryMap; private final StringRedisTemplate redisTemplate; private final String prefix; - private final AttachmentClient client; private Progress progress; - public ATOReportServiceImpl(StringRedisTemplate redisTemplate, @Value("${app.redis.prefix}") String prefix, AttachmentClient client) { + public ATOReportServiceImpl(StringRedisTemplate redisTemplate, @Value("${app.redis.prefix}") String prefix) { this.redisTemplate = redisTemplate; this.prefix = prefix; - this.client = client; } @PostConstruct @@ -150,11 +146,12 @@ public class ATOReportServiceImpl implements ATOReportService { byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); progress.setStatus("Uploading,filesize:" + contentBytes.length); String filename = "royalpay_ato_report_" + new DateTime(from).toString("yyyyMMdd") + "_to_" + new DateTime(to).toString("yyyyMMdd") + ".bttps"; - JSONObject file = client.uploadFile(new ByteArrayInputStream(contentBytes), filename, true); - String fileid = file.getString("fileid"); - logger.info("uploaded ATO report to {}", fileid); - progress.setFileid(fileid); - redisTemplate.boundValueOps(reportFileKey()).set(file.toJSONString(), Duration.ofDays(1)); + File saveFile = new File("/var/log/payment/" + filename); + FileUtils.writeByteArrayToFile(saveFile, contentBytes); + String path = saveFile.getAbsolutePath(); + logger.info("uploaded ATO report to {}", path); + progress.setFileid(path); + redisTemplate.boundValueOps(reportFileKey()).set(path, Duration.ofDays(1)); } catch (Exception e) { progress.setStatus(e.getMessage()); logger.error("output bttps file failed", e); @@ -188,12 +185,12 @@ public class ATOReportServiceImpl implements ATOReportService { public void downloadFile(HttpServletResponse resp) throws IOException { String file = redisTemplate.boundValueOps(reportFileKey()).get(); if (file != null) { - JSONObject fileInfo = JSON.parseObject(file); - String filename = StringUtils.substringAfterLast(fileInfo.getString("url"), "/"); + String filename = FilenameUtils.getName(file); resp.setHeader(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.builder("attachment").filename(filename).build().toString()); resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); - try (OutputStream out = resp.getOutputStream()) { - client.getFileContent(fileInfo.getString("fileid"), out); + try (OutputStream out = resp.getOutputStream(); + InputStream in = new FileInputStream(file)) { + IOUtils.copy(in, out); out.flush(); } } else {