Java 检测无效下载地址(进度版)

/ 默认分类 / 没有评论 / 920浏览
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestFile {


 

    public static void main(String[] args) throws Exception{
        
   // select group_concat(CONCAT('"',download_url,'"'))    from   table 

        List<String> list = Arrays.asList(
        "https://maruifu.cn/pdf/static//702e126d6d714be0979ae27aaa59893b.pdf"
        ,"https://maruifu.cn/pdf/static//98edd35b7e8a493cbeeda2bf4abeb8d8.pdf"
        ,"https://maruifu.cn/pdf/static//92e2360fc7514bd191b1c248de8fda5d.pdf"
        ,"https://maruifu.cn/pdf/static//85faf19d86084a938571907db4f83c4a.pdf");
        List<String> strs = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            printSchedule(  (i + 1)*100/list.size());
            if(!"N".equals(getFlile(list.get(i)))){
                strs.add(getFlile(list.get(i)));
            }

        }
        System.out.println("\n"+"不能下载地址列表:"+strs.toString());

    }




    public static String getFlile(String filepath)throws Exception{
            URL pathUrl = new URL(filepath);
            HttpURLConnection urlcon = (HttpURLConnection) pathUrl.openConnection();
            if(urlcon.getResponseCode()>=400){
                return filepath;
            }
          return "N";
    }



    private static int TOTLE_LENGTH = 100;
    public static void printSchedule(int percent){
        for (int i = 0; i < TOTLE_LENGTH + 10; i++) {
            System.out.print("\b");
        }
        int now = TOTLE_LENGTH * percent / 100;
        for (int i = 0; i < now; i++) {
            System.out.print(">");
        }
        for (int i = 0; i < TOTLE_LENGTH - now; i++) {
            System.out.print(" ");
        }
        System.out.print("  " + percent + "%");
    }
}