如何实现复制网页上的图片

想实现在采集网页的过程中,将网页的图片保存下来,
在新的文章当中直接使用网页上采集下来的图片。
大家帮忙看看如何实现?

应该可以再网上下载然后使用流保存到文件中,使用的时候直接选择文件中的图片就可以吧

从网上找了一段代码,和大家分享一下。这个里面要是能自动的分析html内容就好了

示例创建HttpURLConnection网络连接,并将这个连接获得的网络数据流写道本地磁盘!

示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package imageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**

  • @说明 从网络获取图片到本地
  • @author 崔素强
  • @version 1.0
  • @since / public class GetImage { /*
    • 测试
    • @param args / public static void main(String[] args) { String url = "http://www.baidu.com/img/baidu_sylogo1.gif"; byte[] btImg = getImageFromNetByUrl(url); if(null != btImg && btImg.length > 0){ System.out.println("读取到:" + btImg.length + " 字节"); String fileName = "百度.gif"; writeImageToDisk(btImg, fileName); }else{ System.out.println("没有从该连接获得内容"); } } /*
    • 将图片写入到磁盘
    • @param img 图片数据流
    • @param fileName 文件保存时的名称 / public static void writeImageToDisk(byte[] img, String fileName){ try { File file = new File("D:\" + fileName); FileOutputStream fops = new FileOutputStream(file); fops.write(img); fops.flush(); fops.close(); System.out.println("图片已经写入到D盘"); } catch (Exception e) { e.printStackTrace(); } } /*
    • 根据地址获得数据的字节流
    • @param strUrl 网络连接地址
    • @return / public static byte[] getImageFromNetByUrl(String strUrl){ try { URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();//通过输入流获取图片数据 byte[] btImg = readInputStream(inStream);//得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; } /*
    • 从输入流中获取数据
    • @param inStream 输入流
    • @return
    • @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=inStream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }