Java代码实现下载图片
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class DownloadImg {
public static void main(String[] args) {
URL url;
BufferedInputStream in;
FileOutputStream file;
String httpUrl = "https://b0.bdstatic.com//ugc/server0435b6abefa308d244cbe4d3e9d7e997.jpeg@h_1280";
try {
System.out.println("获取网络图片");
String fileName = httpUrl.substring(httpUrl.lastIndexOf("/") + 1);
String filePath = "C:\\";
url = new URL(httpUrl);
in = new BufferedInputStream(url.openStream());
file = new FileOutputStream(new File(filePath + fileName));
int t;
while ((t = in.read()) != -1) {
file.write(t);
}
file.close();
in.close();
System.out.println("图片获取成功" + i);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}