Commit 69c03978 authored by frank.xa.zhang's avatar frank.xa.zhang

fixed ftp get file too slow issue

parent e6495005
...@@ -321,6 +321,12 @@ ...@@ -321,6 +321,12 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.0</version> <version>1.18.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<!--<dependency>--> <!--<dependency>-->
<!--<groupId>org.eclipse.jetty.aggregate</groupId>--> <!--<groupId>org.eclipse.jetty.aggregate</groupId>-->
<!--<artifactId>jetty-all</artifactId>--> <!--<artifactId>jetty-all</artifactId>-->
......
...@@ -2,6 +2,10 @@ package pwc.taxtech.atms.common.ftp; ...@@ -2,6 +2,10 @@ package pwc.taxtech.atms.common.ftp;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import pwc.taxtech.atms.exception.ServiceException; import pwc.taxtech.atms.exception.ServiceException;
...@@ -16,6 +20,8 @@ public class FtpService { ...@@ -16,6 +20,8 @@ public class FtpService {
String ftpRootPath; String ftpRootPath;
private String requestPrefix = "http://";
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception { public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath; String upPath;
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) { try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) {
...@@ -109,4 +115,28 @@ public class FtpService { ...@@ -109,4 +115,28 @@ public class FtpService {
ftpRootPath = wrapFtpClient.ftpClient.printWorkingDirectory(); ftpRootPath = wrapFtpClient.ftpClient.printWorkingDirectory();
} }
public InputStream getFtpFileWithStaticUrl(String fileUrl) throws IOException {
if (StringUtils.isBlank(fileUrl)) {
return null;
}
if (StringUtils.isNotBlank(config.getFtpHost())) {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(requestPrefix + config.getFtpHost() + "/" + fileUrl);
CloseableHttpResponse response = null;
try {
response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode()==200) {
return response.getEntity().getContent();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//client.close();
//response.close();
}
}
return null;
}
} }
...@@ -68,7 +68,7 @@ public class TemplateController extends BaseController { ...@@ -68,7 +68,7 @@ public class TemplateController extends BaseController {
if (template.getIsSystemType()) { if (template.getIsSystemType()) {
inputStream = new BufferedInputStream(new FileInputStream(templateFile)); inputStream = new BufferedInputStream(new FileInputStream(templateFile));
} else { } else {
inputStream = ftpService.download(templatePath); inputStream = ftpService.getFtpFileWithStaticUrl(templatePath);
} }
//客户端保存的文件名 //客户端保存的文件名
String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx"; String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx";
......
...@@ -23,6 +23,6 @@ public class FTPFileSystemServiceImpl implements FileSystemService { ...@@ -23,6 +23,6 @@ public class FTPFileSystemServiceImpl implements FileSystemService {
@Override @Override
public InputStream downloadUserTemplate(String filePath) throws Exception { public InputStream downloadUserTemplate(String filePath) throws Exception {
return ftpService.download(filePath); return ftpService.getFtpFileWithStaticUrl(filePath);
} }
} }
...@@ -6,12 +6,7 @@ import org.apache.poi.ss.formula.functions.FreeRefFunction; ...@@ -6,12 +6,7 @@ import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder; import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.DefaultUDFFinder; import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -29,14 +24,9 @@ import pwc.taxtech.atms.vat.service.ReportGenerator; ...@@ -29,14 +24,9 @@ import pwc.taxtech.atms.vat.service.ReportGenerator;
import pwc.taxtech.atms.vat.service.impl.report.functions.*; import pwc.taxtech.atms.vat.service.impl.report.functions.*;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.io.InputStream;
import java.util.Date; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static pwc.taxtech.atms.constant.Constant.EMPTY; import static pwc.taxtech.atms.constant.Constant.EMPTY;
...@@ -75,11 +65,11 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen ...@@ -75,11 +65,11 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
List<PeriodCellTemplateConfig> periodCellTemplateConfigList = List<PeriodCellTemplateConfig> periodCellTemplateConfigList =
periodCellTemplateConfigMapper.selectByExample(periodCellTemplateConfigExample); periodCellTemplateConfigMapper.selectByExample(periodCellTemplateConfigExample);
Map<String, String> templateCodeAndPath = new HashMap<>(); //Map<String, String> templateCodeAndPath = new HashMap<>();
periodTemplateList.forEach(a -> templateCodeAndPath.put(a.getCode(), getTemplatePath(a.getPath()))); //periodTemplateList.forEach(a -> templateCodeAndPath.put(a.getCode(), getTemplatePath(a.getPath())));
Optional<Workbook> workbook = createWorkBookWithExcelFileList(templateCodeAndPath); Optional<Workbook> workbook = createWorkBookWithExcelFileList(periodTemplateList);
if (workbook != null) { if (workbook.isPresent()) {
Workbook newWorkbook = workbook.get(); Workbook newWorkbook = workbook.get();
FormulaContext formulaContext = new FormulaContext(); FormulaContext formulaContext = new FormulaContext();
formulaContext.setFormulaAgent(formulaAgent); formulaContext.setFormulaAgent(formulaAgent);
...@@ -248,23 +238,41 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen ...@@ -248,23 +238,41 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
* @param templates 模板code和模板路径 键值对 * @param templates 模板code和模板路径 键值对
* @return 工作簿workbook * @return 工作簿workbook
*/ */
private Optional<Workbook> createWorkBookWithExcelFileList(Map<String, String> templates) { private Optional<Workbook> createWorkBookWithExcelFileList(List<PeriodTemplate> templates) {
Workbook workbook = new XSSFWorkbook(); Workbook workbook = new XSSFWorkbook();
try { try {
String filePath = this.getClass().getResource("").toURI().getPath(); String filePath = this.getClass().getResource("").toURI().getPath();
String tempPath = filePath.substring(0, filePath.indexOf("classes") + "\\classes".length()); String tempPath = filePath.substring(0, filePath.indexOf("classes") + "\\classes".length());
templates.forEach((code, path) -> { templates.forEach(a -> {
File file = new File(tempPath + path); Workbook tWorkbook = null;
File file = null;
if (a.getIsSystemType()) {
file = new File(tempPath + a.getPath());
try { try {
Workbook tWorkbook = WorkbookFactory.create(file); tWorkbook = WorkbookFactory.create(file);
POIUtil.cloneSheet(tWorkbook.getSheetAt(0), workbook.createSheet(code)); } catch (IOException e) {
} catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
e.printStackTrace(); e.printStackTrace();
}
} else {
InputStream is = null;
try {
is = ftpService.getFtpFileWithStaticUrl(a.getPath());
tWorkbook = WorkbookFactory.create(is);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
}
}
POIUtil.cloneSheet(tWorkbook.getSheetAt(0), workbook.createSheet(a.getCode()));
}); });
return Optional.of(workbook); return Optional.of(workbook);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -3,6 +3,7 @@ package pwc.taxtech.atms.vat.service.impl; ...@@ -3,6 +3,7 @@ package pwc.taxtech.atms.vat.service.impl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.dao.*; import pwc.taxtech.atms.dao.*;
import pwc.taxtech.atms.dao.dao.ProjectDao; import pwc.taxtech.atms.dao.dao.ProjectDao;
import pwc.taxtech.atms.service.impl.DistributedIDService; import pwc.taxtech.atms.service.impl.DistributedIDService;
...@@ -82,5 +83,7 @@ public class VatAbstractService { ...@@ -82,5 +83,7 @@ public class VatAbstractService {
public CellCommentMapper cellCommentMapper; public CellCommentMapper cellCommentMapper;
@Autowired @Autowired
public KeyValueConfigMapper keyValueConfigMapper; public KeyValueConfigMapper keyValueConfigMapper;
@Autowired
public FtpService ftpService;
} }
...@@ -34,7 +34,6 @@ import java.util.List; ...@@ -34,7 +34,6 @@ import java.util.List;
/// <param name="keyParam">摘要(未实现)</param> /// <param name="keyParam">摘要(未实现)</param>
public class FSJZ extends FunctionBase implements FreeRefFunction { public class FSJZ extends FunctionBase implements FreeRefFunction {
public FSJZ(FormulaContext formulaContext) { public FSJZ(FormulaContext formulaContext) {
super(formulaContext); super(formulaContext);
} }
......
...@@ -21,7 +21,7 @@ import java.util.List; ...@@ -21,7 +21,7 @@ import java.util.List;
public class FunctionBase { public class FunctionBase {
protected final Logger logger = LoggerFactory.getLogger(this.getClass()); protected final Logger logger = LoggerFactory.getLogger(this.getClass());
public static final BigDecimal[] defaultTaxRates = {new BigDecimal("0.17"), new BigDecimal("0.13") public static final BigDecimal[] defaultTaxRates = {new BigDecimal("0.17"), new BigDecimal("0.13")
, new BigDecimal("0.11"), new BigDecimal("0.06"), new BigDecimal(" 0.05"), new BigDecimal("0.03")}; , new BigDecimal("0.11"), new BigDecimal("0.06"), new BigDecimal("0.05"), new BigDecimal("0.03")};
protected FormulaContext formulaContext; protected FormulaContext formulaContext;
final FormulaAgent agent; final FormulaAgent agent;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment