DownloadUtils.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package cn.keking.utils;
  2. import cn.keking.config.ConfigConstants;
  3. import cn.keking.model.FileAttribute;
  4. import cn.keking.model.ReturnResponse;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import io.mola.galimatias.GalimatiasParseException;
  7. import org.apache.commons.io.FileUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.http.HttpMethod;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  13. import org.springframework.util.ObjectUtils;
  14. import org.springframework.util.StringUtils;
  15. import org.springframework.web.client.RequestCallback;
  16. import org.springframework.web.client.RestClientException;
  17. import org.springframework.web.client.RestTemplate;
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.net.URI;
  22. import java.net.URL;
  23. import java.net.URLEncoder;
  24. import java.util.Arrays;
  25. import java.util.Map;
  26. import java.util.UUID;
  27. import static cn.keking.utils.KkFileUtils.isFtpUrl;
  28. import static cn.keking.utils.KkFileUtils.isHttpUrl;
  29. /**
  30. * @author yudian-it
  31. */
  32. public class DownloadUtils {
  33. private final static Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
  34. private static final String fileDir = ConfigConstants.getFileDir();
  35. private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
  36. private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
  37. private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
  38. private static final RestTemplate restTemplate = new RestTemplate();
  39. private static final ObjectMapper mapper = new ObjectMapper();
  40. /**
  41. * @param fileAttribute fileAttribute
  42. * @param fileName 文件名
  43. * @return 本地文件绝对路径
  44. */
  45. public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
  46. // 忽略ssl证书
  47. String urlStr = null;
  48. try {
  49. SslUtils.ignoreSsl();
  50. urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20").replaceAll(" ", "%20");
  51. } catch (Exception e) {
  52. logger.error("忽略SSL证书异常:", e);
  53. }
  54. ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
  55. String realPath = getRelFilePath(fileName, fileAttribute);
  56. // 判断是否非法地址
  57. if (KkFileUtils.isIllegalFileName(realPath)) {
  58. response.setCode(1);
  59. response.setContent(null);
  60. response.setMsg("下载失败:文件名不合法!" + urlStr);
  61. return response;
  62. }
  63. if (!KkFileUtils.isAllowedUpload(realPath)) {
  64. response.setCode(1);
  65. response.setContent(null);
  66. response.setMsg("下载失败:不支持的类型!" + urlStr);
  67. return response;
  68. }
  69. if (fileAttribute.isCompressFile()) { //压缩包文件 直接赋予路径 不予下载
  70. response.setContent(fileDir + fileName);
  71. response.setMsg(fileName);
  72. return response;
  73. }
  74. // 如果文件是否已经存在、且不强制更新,则直接返回文件路径
  75. if (KkFileUtils.isExist(realPath) && !fileAttribute.forceUpdatedCache()) {
  76. response.setContent(realPath);
  77. response.setMsg(fileName);
  78. return response;
  79. }
  80. try {
  81. URL url = WebUtils.normalizedURL(urlStr);
  82. if (!fileAttribute.getSkipDownLoad()) {
  83. if (isHttpUrl(url)) {
  84. File realFile = new File(realPath);
  85. SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory();
  86. //连接超时10秒,默认无限制,单位:毫秒
  87. httpFactory.setConnectTimeout(60 * 1000);
  88. //读取超时5秒,默认无限限制,单位:毫秒
  89. httpFactory.setReadTimeout(60 * 1000);
  90. restTemplate.setRequestFactory(httpFactory);
  91. RequestCallback requestCallback = request -> {
  92. request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
  93. String proxyAuthorization = fileAttribute.getKkProxyAuthorization();
  94. if(StringUtils.hasText(proxyAuthorization)){
  95. Map<String,String> proxyAuthorizationMap = mapper.readValue(proxyAuthorization, Map.class);
  96. proxyAuthorizationMap.forEach((key, value) -> request.getHeaders().set(key, value));
  97. }
  98. };
  99. try {
  100. restTemplate.execute(url.toURI(), HttpMethod.GET, requestCallback, fileResponse -> {
  101. FileUtils.copyToFile(fileResponse.getBody(), realFile);
  102. return null;
  103. });
  104. } catch (Exception e) {
  105. response.setCode(1);
  106. response.setContent(null);
  107. response.setMsg("下载失败:" + e);
  108. return response;
  109. }
  110. } else if (isFtpUrl(url)) {
  111. String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
  112. String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
  113. String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
  114. FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
  115. } else {
  116. response.setCode(1);
  117. response.setMsg("url不能识别url" + urlStr);
  118. }
  119. }
  120. response.setContent(realPath);
  121. response.setMsg(fileName);
  122. return response;
  123. } catch (IOException | GalimatiasParseException e) {
  124. logger.error("文件下载失败,url:{}", urlStr);
  125. response.setCode(1);
  126. response.setContent(null);
  127. if (e instanceof FileNotFoundException) {
  128. response.setMsg("文件不存在!!!");
  129. } else {
  130. response.setMsg(e.getMessage());
  131. }
  132. return response;
  133. }
  134. }
  135. /**
  136. * 获取真实文件绝对路径
  137. *
  138. * @param fileName 文件名
  139. * @return 文件路径
  140. */
  141. private static String getRelFilePath(String fileName, FileAttribute fileAttribute) {
  142. String type = fileAttribute.getSuffix();
  143. if (null == fileName) {
  144. UUID uuid = UUID.randomUUID();
  145. fileName = uuid + "." + type;
  146. } else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
  147. fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
  148. }
  149. String realPath = fileDir + fileName;
  150. File dirFile = new File(fileDir);
  151. if (!dirFile.exists() && !dirFile.mkdirs()) {
  152. logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir);
  153. }
  154. return realPath;
  155. }
  156. }