DownloadUtils.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. String fileKey = fileAttribute.getFileKey();
  47. // 忽略ssl证书
  48. String urlStr = null;
  49. try {
  50. SslUtils.ignoreSsl();
  51. urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20").replaceAll(" ", "%20");
  52. } catch (Exception e) {
  53. logger.error("忽略SSL证书异常:", e);
  54. }
  55. ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
  56. String realPath = getRelFilePath(fileName, fileAttribute);
  57. // 判断是否非法地址
  58. if (KkFileUtils.isIllegalFileName(realPath)) {
  59. response.setCode(1);
  60. response.setContent(null);
  61. response.setMsg("下载失败:文件名不合法!" + urlStr);
  62. return response;
  63. }
  64. if (!KkFileUtils.isAllowedUpload(realPath)) {
  65. response.setCode(1);
  66. response.setContent(null);
  67. response.setMsg("下载失败:不支持的类型!" + urlStr);
  68. return response;
  69. }
  70. if (!ObjectUtils.isEmpty(fileKey)) { //压缩包文件 直接赋予路径 不予下载
  71. response.setContent(fileDir + fileName);
  72. response.setMsg(fileName);
  73. return response;
  74. }
  75. // 如果文件是否已经存在、且不强制更新,则直接返回文件路径
  76. if (KkFileUtils.isExist(realPath) && !fileAttribute.forceUpdatedCache()) {
  77. response.setContent(realPath);
  78. response.setMsg(fileName);
  79. return response;
  80. }
  81. try {
  82. URL url = WebUtils.normalizedURL(urlStr);
  83. if (!fileAttribute.getSkipDownLoad()) {
  84. if (isHttpUrl(url)) {
  85. File realFile = new File(realPath);
  86. SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory();
  87. //连接超时10秒,默认无限制,单位:毫秒
  88. httpFactory.setConnectTimeout(60 * 1000);
  89. //读取超时5秒,默认无限限制,单位:毫秒
  90. httpFactory.setReadTimeout(60 * 1000);
  91. restTemplate.setRequestFactory(httpFactory);
  92. RequestCallback requestCallback = request -> {
  93. request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
  94. String proxyAuthorization = fileAttribute.getKkProxyAuthorization();
  95. if(StringUtils.hasText(proxyAuthorization)){
  96. Map<String,String> proxyAuthorizationMap = mapper.readValue(proxyAuthorization, Map.class);
  97. proxyAuthorizationMap.forEach((key, value) -> request.getHeaders().set(key, value));
  98. }
  99. };
  100. try {
  101. URI uri = URI.create(urlStr);
  102. restTemplate.execute(uri, HttpMethod.GET, requestCallback, fileResponse -> {
  103. FileUtils.copyToFile(fileResponse.getBody(), realFile);
  104. return null;
  105. });
  106. } catch (Exception e) {
  107. response.setCode(1);
  108. response.setContent(null);
  109. response.setMsg("下载失败:" + e);
  110. return response;
  111. }
  112. } else if (isFtpUrl(url)) {
  113. String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
  114. String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
  115. String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
  116. FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
  117. } else {
  118. response.setCode(1);
  119. response.setMsg("url不能识别url" + urlStr);
  120. }
  121. }
  122. response.setContent(realPath);
  123. response.setMsg(fileName);
  124. return response;
  125. } catch (IOException | GalimatiasParseException e) {
  126. logger.error("文件下载失败,url:{}", urlStr);
  127. response.setCode(1);
  128. response.setContent(null);
  129. if (e instanceof FileNotFoundException) {
  130. response.setMsg("文件不存在!!!");
  131. } else {
  132. response.setMsg(e.getMessage());
  133. }
  134. return response;
  135. }
  136. }
  137. /**
  138. * 获取真实文件绝对路径
  139. *
  140. * @param fileName 文件名
  141. * @return 文件路径
  142. */
  143. private static String getRelFilePath(String fileName, FileAttribute fileAttribute) {
  144. String type = fileAttribute.getSuffix();
  145. if (null == fileName) {
  146. UUID uuid = UUID.randomUUID();
  147. fileName = uuid + "." + type;
  148. } else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
  149. fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
  150. }
  151. String realPath = fileDir + fileName;
  152. File dirFile = new File(fileDir);
  153. if (!dirFile.exists() && !dirFile.mkdirs()) {
  154. logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir);
  155. }
  156. return realPath;
  157. }
  158. }