JAVA实现把网页的图片下载下来

版权声明:ThomasKwan https://blog.csdn.net/thomassamul/article/details/86623512

在Java下抓取网页上的图片并保存到本地文件夹中

技术实现:

import java.net.URL;

import java.net.MalformedURLException;

import java.io.IOException;

import java.io.FileNotFoundException;

import java.io.BufferedInputStream;

import java.io.FileOutputStream;

import java.io.File;

实现代码:

public class TestGetImageFromUrl {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String httpUrl = "http://www.a3gs.com/关于我们.files/image002.gif";

URL url;

BufferedInputStream in;

FileOutputStream file;

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("图片获取成功");

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

} 

猜你喜欢

转载自blog.csdn.net/thomassamul/article/details/86623512