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();
}
}
}
07-04
554
06-19
10-24
428