多线程方式根据图片url链接批量下载图片到本地指定文件夹

总就三个东西,如下图:第一个txt文件就是有图片的url链接地址

https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11572548.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11593467.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11631922.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-12203460.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-12240136.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-10092819.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-10519162.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-10977780.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-11311195.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-11954484.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-12072057.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-12072057.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-4542338.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-6323680.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-6873171.jpeg
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/s/pexels-photo-7409835.jpeg

第二个就是 properties文件,指定线程数量和本地的下载文件夹绝对路径:

注意,thread-num=6,6的后面不能有空格。正确的路径写法:target-dir = e:/test/1111/       错误的:target-dir = e:/test/1111       or    target-dir = e:test/1111 

第三个就是downloader类:

 最终的bug改好了,原因是把新建文件夹

一级文件夹:dir.mkdir()    多级文件夹: dir.mkdirs()

,错误写成了新建文件:dir.createNewFile()

 

完整代码如下:

package qiu.downloader;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Downloader {
    private int threadNum = 0;
    public static void main(String[] args) throws IOException {
        Downloader downloader = new Downloader();
//        String urlStr = new String("https://img2.baidu.com/it/u=419672121,2062370122&fm=253&fmt=auto&app=138&f=PNG?w=500&h=378");
//        downloader.urlDown(urlStr,"e:/test/1109/a.jpg");
        String pictureTxt = new String("D:\\ideaWorkspace\\chapter11month\\io\\src\\picture.txt");
        String configDir = new String("D:\\ideaWorkspace\\chapter11month\\io\\src\\config.properties");
        downloader.multiDownload(pictureTxt,configDir);

    }

    /**
     * 根据完整下载资源主要是图片到本地
     * @param urlStr 网址
     * @param dir  目录路径字符
     */
    public void urlDown(String urlStr,String dir){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        File file = new File(dir);//创建文件,dir=绝对路径+图片名称后缀
        try {
            URL url = new URL(urlStr);
            URLConnection urlConnection = url.openConnection();
            inputStream = urlConnection.getInputStream();
            outputStream = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int n = 0;
            String str = null;
            while((n = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,n);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }


        }
    }

    /**
     *
     * @param pictureTxt picture.txt绝对地址,"D:\\ideaWorkspace\\chapter11month\\io\\src\\picture.txt"
     * @param configDir  config.properties的绝对路径,D:\ideaWorkspace\chapter11month\io\src\config.properties
     */
    public void multiDownload(String pictureTxt,String configDir) throws IOException {
        //读取pictureTxt的每一行内容,也就是每一行一个下载地址
        FileReader fileReader = new FileReader(pictureTxt);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String str = null;
        List<String> list = new ArrayList<>();
        //网络上的图片下载链接地址
        while ((str = bufferedReader.readLine()) != null){
            list.add(str);
        }
        //读取config文件,即threadNum和targetDir
        FileReader configReader = new FileReader(configDir);
        Properties properties = new Properties();
        properties.load(configReader);
        String threadNum = properties.getProperty("thread-num");
        this.threadNum = Integer.parseInt(threadNum);
        String targetDir = properties.getProperty("target-dir");
        //先创建一个目标目录对象,保存在本地的目录
        File dir = new File(targetDir);
        //dir == null
        if (!dir.exists()){
            System.out.println("目录不存在,现在开始创建文件夹");
           // boolean isCreateFile = dir.createNewFile();//这是新建文件
            boolean mkdir = dir.mkdir();
            if (mkdir) {
                System.out.println("目录创建成功,下载图片中");
            }else {
                System.out.println("目录创建失败");
            }
        }else {
            System.out.println("目录存在,无需创建,下载图片中");
        }
        //开始创建多线程进行批量下载
        ExecutorService executorService = Executors.newFixedThreadPool(this.threadNum);
        Downloader downloader = this;
        for (String picUrl : list) {
            System.out.println("picUrl:"+picUrl);
            int index = picUrl.lastIndexOf("/");
            String targetJpg = picUrl.substring(index+1);
            String lastDir = new String(targetDir+"/"+targetJpg);//注意一定要加上    /
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    downloader.urlDown(picUrl,lastDir);
                }
            });
        }
        //关闭线程池和字节输入流
        executorService.shutdown();
        bufferedReader.close();
    }
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
运行环境 .NET Framework2.0 开发工具 Microsoft Visual Studio 2005 二. 部分代码说明(主要讲解异步分析和下载): 异步分析下载采取的策略是同时分析同时下载,即未等待数据全部分析完毕就开始把已经分析出来的图片链接开始下载下载成功的均在List框链接前面划上了√ ,未能下载图片有可能是分析错误或者是下载异常。 1. 异步分析部分代码 /// /// 异步分析下载 /// private void AsyncAnalyzeAndDownload(string url, string savePath) { this.uriString = url; this.savePath = savePath; #region 分析计时开始 count = 0; count1 = 0; freq = 0; result = 0; QueryPerformanceFrequency(ref freq); QueryPerformanceCounter(ref count); #endregion using (WebClient wClient = new WebClient()) { AutoResetEvent waiter = new AutoResetEvent(false); wClient.Credentials = CredentialCache.DefaultCredentials; wClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(AsyncURIAnalyze); wClient.DownloadDataAsync(new Uri(uriString), waiter); //waiter.WaitOne(); //阻止当前线程,直到收到信号 } } /// /// 异步分析 /// protected void AsyncURIAnalyze(Object sender, DownloadDataCompletedEventArgs e) { AutoResetEvent waiter = (AutoResetEvent)e.UserState; try { if (!e.Cancelled && e.Error == null) { string dnDir = string.Empty; string domainName = string.Empty; string uri = uriString; //获得域名 http://www.sina.com/ Match match = Regex.Match(uri, @"((http(s)?://)?)+[\w-.]+[^/]");//, RegexOptions.IgnoreCase domainName = match.Value; //获得域名最深层目录 http://www.sina.com/mail/ if (domainName.Equals(uri

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值