java实现下载网络图片
package com.gylhaut.picture;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class PictureDownload {
public static void main(String[] args) {
String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546100952419&di=4b9466f689800898b761d411c0eed2d2&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F14ce36d3d539b600d3924a1feb50352ac65cb73e.jpg";
downloadPicture(url);
}
//链接url下载图片
private static void downloadPicture(String urlList) {
URL url = null;
int imageNumber = 0;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
String imageName = "E:/test.jpg";
FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context=output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
版权声明:本文为gylhaut原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。