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 @@
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</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>-->
<!--<groupId>org.eclipse.jetty.aggregate</groupId>-->
<!--<artifactId>jetty-all</artifactId>-->
......
......@@ -2,6 +2,10 @@ package pwc.taxtech.atms.common.ftp;
import org.apache.commons.lang3.StringUtils;
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.stereotype.Service;
import pwc.taxtech.atms.exception.ServiceException;
......@@ -16,6 +20,8 @@ public class FtpService {
String ftpRootPath;
private String requestPrefix = "http://";
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath;
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) {
......@@ -109,4 +115,28 @@ public class FtpService {
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 {
if (template.getIsSystemType()) {
inputStream = new BufferedInputStream(new FileInputStream(templateFile));
} else {
inputStream = ftpService.download(templatePath);
inputStream = ftpService.getFtpFileWithStaticUrl(templatePath);
}
//客户端保存的文件名
String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx";
......
......@@ -23,6 +23,6 @@ public class FTPFileSystemServiceImpl implements FileSystemService {
@Override
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;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -29,14 +24,9 @@ import pwc.taxtech.atms.vat.service.ReportGenerator;
import pwc.taxtech.atms.vat.service.impl.report.functions.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import static pwc.taxtech.atms.constant.Constant.EMPTY;
......@@ -75,11 +65,11 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
List<PeriodCellTemplateConfig> periodCellTemplateConfigList =
periodCellTemplateConfigMapper.selectByExample(periodCellTemplateConfigExample);
Map<String, String> templateCodeAndPath = new HashMap<>();
periodTemplateList.forEach(a -> templateCodeAndPath.put(a.getCode(), getTemplatePath(a.getPath())));
Optional<Workbook> workbook = createWorkBookWithExcelFileList(templateCodeAndPath);
//Map<String, String> templateCodeAndPath = new HashMap<>();
//periodTemplateList.forEach(a -> templateCodeAndPath.put(a.getCode(), getTemplatePath(a.getPath())));
Optional<Workbook> workbook = createWorkBookWithExcelFileList(periodTemplateList);
if (workbook != null) {
if (workbook.isPresent()) {
Workbook newWorkbook = workbook.get();
FormulaContext formulaContext = new FormulaContext();
formulaContext.setFormulaAgent(formulaAgent);
......@@ -248,23 +238,41 @@ public class ReportGeneratorImpl extends VatAbstractService implements ReportGen
* @param templates 模板code和模板路径 键值对
* @return 工作簿workbook
*/
private Optional<Workbook> createWorkBookWithExcelFileList(Map<String, String> templates) {
private Optional<Workbook> createWorkBookWithExcelFileList(List<PeriodTemplate> templates) {
Workbook workbook = new XSSFWorkbook();
try {
String filePath = this.getClass().getResource("").toURI().getPath();
String tempPath = filePath.substring(0, filePath.indexOf("classes") + "\\classes".length());
templates.forEach((code, path) -> {
File file = new File(tempPath + path);
try {
Workbook tWorkbook = WorkbookFactory.create(file);
POIUtil.cloneSheet(tWorkbook.getSheetAt(0), workbook.createSheet(code));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
templates.forEach(a -> {
Workbook tWorkbook = null;
File file = null;
if (a.getIsSystemType()) {
file = new File(tempPath + a.getPath());
try {
tWorkbook = WorkbookFactory.create(file);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
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) {
e.printStackTrace();
}
}
}
}
POIUtil.cloneSheet(tWorkbook.getSheetAt(0), workbook.createSheet(a.getCode()));
});
return Optional.of(workbook);
} catch (Exception e) {
......
......@@ -3,6 +3,7 @@ package pwc.taxtech.atms.vat.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.dao.*;
import pwc.taxtech.atms.dao.dao.ProjectDao;
import pwc.taxtech.atms.service.impl.DistributedIDService;
......@@ -82,5 +83,7 @@ public class VatAbstractService {
public CellCommentMapper cellCommentMapper;
@Autowired
public KeyValueConfigMapper keyValueConfigMapper;
@Autowired
public FtpService ftpService;
}
......@@ -34,7 +34,6 @@ import java.util.List;
/// <param name="keyParam">摘要(未实现)</param>
public class FSJZ extends FunctionBase implements FreeRefFunction {
public FSJZ(FormulaContext formulaContext) {
super(formulaContext);
}
......
......@@ -21,7 +21,7 @@ import java.util.List;
public class FunctionBase {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
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;
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