批量下载url图片

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class DownloadPicFromUrlListUtil {
    public static void main(String[] args) {
        getDirectory("E://ATest/July/url");// 存放url的文件,一行一个url
    }

    public static void getDirectory(String pathname) {
        Path path = new File(pathname).toPath();
        try {
            DirectoryStream<Path> children = Files.newDirectoryStream(path);
            if (children != null) {
                int i = 0;
                for (Path child : children) {
                    // System.out.println("Dir==>"+child.toAbsolutePath());
                    StringBuilder result = new StringBuilder();
                    BufferedReader br = new BufferedReader(new FileReader(new File(child.toString())));// 构造一个BufferedReader类来读取文件
                    String s = null;
                    while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
//                        System.out.println(s);
                        if (s.contains("http")) {
                            int last = s.lastIndexOf("/");
                            String dicName = "E:\\ATest\\July\\pic\\" + s.substring(27, last).replace("/","\\");
//                            System.out.println(dicName);
                            String fileName = s.substring(last + 1);
//                            System.out.println(fileName);
                            String savePath = dicName + "\\" + fileName;
                            File file = new File(dicName);
                            file.mkdirs();
                            downloadPicture(s, savePath);
                        }
//                        result.append(System.lineSeparator() + s);
                    }
                    br.close();
//                    System.out.println(result.toString().replaceAll("\"", ""));
                    i++;
                }
//                System.out.println(i);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //链接url下载图片
    private static void downloadPicture(String urlList, String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
运行环境 .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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值