DownloadUtil.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.fdkankan.download.util;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.thread.ThreadUtil;
  4. import cn.hutool.core.util.RuntimeUtil;
  5. import cn.hutool.http.HttpUtil;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import com.fdkankan.common.util.CmdUtils;
  8. import java.io.File;
  9. /**
  10. * @author Xiewj
  11. * @date 2024/1/12
  12. */
  13. public class DownloadUtil {
  14. public final static String WGET_CMD = "wget -t 10 -N -O @out @url";
  15. public static void downFile(String url, String path) throws Exception {
  16. String cmd = WGET_CMD.replace("@out",path).replace("@url",url);
  17. RuntimeUtil.exec(cmd);
  18. }
  19. public static void downloadFile(String url, String path,int index){
  20. File file = new File(path);
  21. if(!file.getParentFile().exists()){
  22. file.getParentFile().mkdirs();
  23. }
  24. try {
  25. if(FileUtil.size(new File(path)) == 0 ){
  26. while (index<=50){
  27. index++;
  28. DownloadUtil.downFile(url,path);
  29. if (FileUtil.size(new File(path))>0){
  30. return;
  31. }
  32. ThreadUtil.safeSleep(500);
  33. // HttpUtil.downloadFileFromUrl(url,path);
  34. }
  35. }
  36. } catch (Exception e) {
  37. throw new BusinessException(-1,"下载报错停止,"+url+","+path);
  38. }
  39. }
  40. }