WebFileDownLoadUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.jkcredit.invoice.util;
  2. import com.jkcredit.invoice.model.vo.SearchInvoiceResultVo;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.util.CollectionUtils;
  8. import org.springframework.web.client.RestTemplate;
  9. import org.springframework.web.util.UriComponentsBuilder;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14. import java.util.Map;
  15. import java.util.Objects;
  16. /**
  17. * @description:
  18. * @author: sunzhaoning
  19. * @create: 2019-07-12 09:30
  20. * @version: V1.0
  21. **/
  22. @Component
  23. @Slf4j
  24. public class WebFileDownLoadUtils {
  25. /**
  26. * 使用自定义的httpclient的restTemplate
  27. */
  28. @Autowired
  29. private RestTemplate httpClientTemplate;
  30. /**
  31. * 下载小文件,采用字节数组的方式,直接将所有返回都放入内存中,容易引发内存溢出
  32. *
  33. * @param url
  34. * @param targetDir
  35. */
  36. public void downloadLittleFileToPath(String url, String targetDir, SearchInvoiceResultVo searchInvoiceResult, String batchNumber) {
  37. downloadLittleFileToPath(url, targetDir, null, searchInvoiceResult, batchNumber);
  38. }
  39. /**
  40. * 下载小文件,直接将所有返回都放入内存中,容易引发内存溢出
  41. *
  42. * @param url
  43. * @param targetDir
  44. */
  45. public void downloadLittleFileToPath(String url, String targetDir, Map<String, String> params, SearchInvoiceResultVo searchInvoiceResult, String batchNumber) {
  46. // Instant now = Instant.now();
  47. String completeUrl = addGetQueryParam(url, params);
  48. ResponseEntity<byte[]> rsp = httpClientTemplate.getForEntity(completeUrl, byte[].class);
  49. // log.info("[下载文件] [状态码] code:{}", rsp.getStatusCode());
  50. try {
  51. String path = getAndCreateDownloadDir(targetDir, searchInvoiceResult, batchNumber);
  52. Files.write(Paths.get(path), Objects.requireNonNull(rsp.getBody(), "未获取到下载文件"));
  53. } catch (IOException e) {
  54. log.error("[下载文件] 写入失败:", e);
  55. }
  56. // log.info("[下载文件] 完成,耗时:{}", ChronoUnit.MILLIS.between(now, Instant.now()));
  57. }
  58. /**
  59. * 拼接get请求参数
  60. *
  61. * @param url
  62. * @param params
  63. * @return
  64. */
  65. private String addGetQueryParam(String url, Map<String, String> params) {
  66. UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
  67. if (!CollectionUtils.isEmpty(params)) {
  68. for (Map.Entry<String, ?> varEntry : params.entrySet()) {
  69. uriComponentsBuilder.queryParam(varEntry.getKey(), varEntry.getValue());
  70. }
  71. }
  72. return uriComponentsBuilder.build().encode().toString();
  73. }
  74. /**
  75. * 创建或获取下载文件夹的路径
  76. * <p>
  77. * // * @param url
  78. *
  79. * @param targetDir
  80. * @return
  81. */
  82. public String getAndCreateDownloadDir(String targetDir, SearchInvoiceResultVo searchInvoiceResult, String batchNumber) throws IOException {
  83. String filename = searchInvoiceResult.getWaybillNum() + "_" + searchInvoiceResult.getInvoiceCode() + searchInvoiceResult.getInvoiceNum();
  84. targetDir = targetDir + File.separator + batchNumber;
  85. if (!Files.exists(Paths.get(targetDir))) {
  86. Files.createDirectories(Paths.get(targetDir));
  87. }
  88. return targetDir + File.separator + filename + ".pdf";
  89. }
  90. }