直接上代码:
package com.lemon.photo;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadPhotoUrl {
public static void main(String[] args) {
String url =
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1569407230663&di=51db9c03ac5432c1cdf94cd4fdabb5a4&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fblog%2F201310%2F18%2F20131018213535_2yTwn.jpeg";
String path="d:/pic.png";
downloadPicture(url,path);
}
//链接url下载图片
/**
* 功能描述:
* @param: urlList 图片url地址
* @param: path 下载到本地的路径
* @return: void
* @author: lemon
* @since: 2019/9/27 0027
*/
private static void downloadPicture(String urlList,String path) {
try {
URL 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();
}
}
}
还没有评论,来说两句吧...