DownloadUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 io.mola.galimatias.GalimatiasParseException;
  6. import org.apache.commons.io.FileUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.util.StringUtils;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.net.URL;
  14. import java.util.UUID;
  15. import static cn.keking.utils.KkFileUtils.isFtpUrl;
  16. import static cn.keking.utils.KkFileUtils.isHttpUrl;
  17. /**
  18. * @author yudian-it
  19. */
  20. public class DownloadUtils {
  21. private final static Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
  22. private static final String fileDir = ConfigConstants.getFileDir();
  23. private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
  24. private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
  25. private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
  26. /**
  27. * @param fileAttribute fileAttribute
  28. * @param fileName 文件名
  29. * @return 本地文件绝对路径
  30. */
  31. public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
  32. // 忽略ssl证书
  33. String urlStr = null;
  34. try {
  35. SslUtils.ignoreSsl();
  36. urlStr = fileAttribute.getUrl().replaceAll("\\+", "%20");
  37. } catch (Exception e) {
  38. logger.error("忽略SSL证书异常:", e);
  39. }
  40. ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
  41. String realPath = getRelFilePath(fileName, fileAttribute);
  42. if (!KkFileUtils.isAllowedUpload(realPath)) {
  43. response.setCode(1);
  44. response.setContent(null);
  45. response.setMsg("下载失败:不支持的类型!" + urlStr);
  46. return response;
  47. }
  48. assert urlStr != null;
  49. if (urlStr.contains("?fileKey=")) {
  50. response.setContent(fileDir + fileName);
  51. response.setMsg(fileName);
  52. return response;
  53. }
  54. if(!StringUtils.hasText(realPath)){
  55. response.setCode(1);
  56. response.setContent(null);
  57. response.setMsg("下载失败:文件名不合法!" + urlStr);
  58. return response;
  59. }
  60. if(realPath.equals("cunzhai")){
  61. response.setContent(fileDir + fileName);
  62. response.setMsg(fileName);
  63. return response;
  64. }
  65. try {
  66. URL url = WebUtils.normalizedURL(urlStr);
  67. if (!fileAttribute.getSkipDownLoad()) {
  68. if (isHttpUrl(url)) {
  69. File realFile = new File(realPath);
  70. FileUtils.copyURLToFile(url, realFile);
  71. } else if (isFtpUrl(url)) {
  72. String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
  73. String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
  74. String ftpControlEncoding = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
  75. FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
  76. } else {
  77. response.setCode(1);
  78. response.setMsg("url不能识别url" + urlStr);
  79. }
  80. }
  81. response.setContent(realPath);
  82. response.setMsg(fileName);
  83. return response;
  84. } catch (IOException | GalimatiasParseException e) {
  85. logger.error("文件下载失败,url:{}", urlStr);
  86. response.setCode(1);
  87. response.setContent(null);
  88. if (e instanceof FileNotFoundException) {
  89. response.setMsg("文件不存在!!!");
  90. } else {
  91. response.setMsg(e.getMessage());
  92. }
  93. return response;
  94. }
  95. }
  96. /**
  97. * 获取真实文件绝对路径
  98. *
  99. * @param fileName 文件名
  100. * @return 文件路径
  101. */
  102. private static String getRelFilePath(String fileName, FileAttribute fileAttribute) {
  103. String type = fileAttribute.getSuffix();
  104. if (null == fileName) {
  105. UUID uuid = UUID.randomUUID();
  106. fileName = uuid + "." + type;
  107. } else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】)
  108. fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
  109. }
  110. // 判断是否非法地址
  111. if (KkFileUtils.isIllegalFileName(fileName)) {
  112. return null;
  113. }
  114. String realPath = fileDir + fileName;
  115. File dirFile = new File(fileDir);
  116. if (!dirFile.exists() && !dirFile.mkdirs()) {
  117. logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir);
  118. }
  119. Boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
  120. //判断是否启用强制更新功能如果启用 文件必须重新下载
  121. if (null == forceUpdatedCache || !forceUpdatedCache) {
  122. // 文件已在本地存在,跳过文件下载
  123. File realFile = new File(realPath);
  124. if (realFile.exists()) {
  125. fileAttribute.setSkipDownLoad(true);
  126. return "cunzhai"; //这里给的值是不能修改的 对应的是下载方法里面有个强制输出地址的
  127. }
  128. }
  129. return realPath;
  130. }
  131. }