123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package com.fdkankan.download.util;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.thread.ThreadUtil;
- import cn.hutool.core.util.RuntimeUtil;
- import cn.hutool.http.HttpUtil;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.common.util.CmdUtils;
- import java.io.File;
- /**
- * @author Xiewj
- * @date 2024/1/12
- */
- public class DownloadUtil {
- public final static String WGET_CMD = "wget -t 10 -N -O @out @url";
- public static void downFile(String url, String path) throws Exception {
- String cmd = WGET_CMD.replace("@out",path).replace("@url",url);
- RuntimeUtil.exec(cmd);
- }
- public static void downloadFile(String url, String path,int index){
- File file = new File(path);
- if(!file.getParentFile().exists()){
- file.getParentFile().mkdirs();
- }
- try {
- if(FileUtil.size(new File(path)) == 0 ){
- while (index<=50){
- index++;
- DownloadUtil.downFile(url,path);
- if (FileUtil.size(new File(path))>0){
- return;
- }
- ThreadUtil.safeSleep(500);
- // HttpUtil.downloadFileFromUrl(url,path);
- }
- }
- } catch (Exception e) {
- throw new BusinessException(-1,"下载报错停止,"+url+","+path);
- }
- }
- }
|