• 批处理 使用500个线程并发下载远程文件


    工程包文件:commons-io-1.1.jar、config.properties、download.jar、start.bat

    说明:借助Apache Commons IO工具包(commons-io-1.1.jar)来简单实现文件(夹)的复制、移动、删除、获取大小等操作

    config.properties 文件内容:

    #远程服务器下载url
    remoteUrl=http://localhost:8080/test/download/a.mp3
    #下载文件保存位置如F:\download
    saveFileUrl=F
    #并发下载线程数
    threadNums=10

    start.bat文件内容:

    set classpath=download.jar;commons-io-1.1.jar
    java FileSave

    pause

    download.jar 文件主class文件源码: 

    import java.io.File;
    import java.net.URL;
    import java.util.Random;

    import org.apache.commons.io.FileUtils;

    public class FileSave
    {
     static String remoteUrl = "http://192.168.100.15:8080/myweb/001232.mp3";
     static String saveFileUrl = "D:/download/";

     public static void main(String[] args)
     {
      for (int i = 0; i < 500; i++)
      {
       new Thread(new myThread(remoteUrl, saveFileUrl)).start();
      }
     }

    }

    class myThread implements Runnable
    {
     int NUM = 1;

     String remoteUrl = "";
     String saveFileUrl = "";

     public myThread(String remoteUrl, String saveFileUrl)
     {
      this.remoteUrl = remoteUrl;
      this.saveFileUrl = saveFileUrl;
     }

     public void run()
     {
      downloadFromUrl(remoteUrl, saveFileUrl);
     }

     /**
      * 文件下载的方法
      */
     public static String downloadFromUrl(String url, String dir)
     {
      String fileName = "";

      try
      {
       URL httpurl = new URL(url);
       String[] us = url.split("/");
       fileName = us[us.length - 1];
       String ramdom = System.currentTimeMillis() + ""
         + new Random().nextInt(100);
       fileName = ramdom + "_" + fileName;
       System.out.println("fileName:" + fileName);
       File f = new File(dir + fileName);
       FileUtils.copyURLToFile(httpurl, f);
      } catch (Exception e)
      {
       e.printStackTrace();
       return "Fault!";
      }

      return fileName;
     }

    }

  • 相关阅读:
    Angular2 组件通信
    vue跨组件通信的几种方法
    Angular React 和 Vue的比较
    vue对比其他框架
    ReactJS 生命周期、数据流与事件
    LeetCode 1089. 复写零(Duplicate Zeros) 72
    LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
    12
    11
    10
  • 原文地址:https://www.cnblogs.com/qqzy168/p/2666481.html
Copyright © 2020-2023  润新知