从一个网页中获取图片

 GetImage类:

怎么说呢,如果你不研究底层一点的东西(其实这里指的是htmlpage类),直接调用还是比较简单的。

代码如下:

  1. import java.awt.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import com.heaton.bot.*;
  6. /**
  7.  * This program accepts a URL as input and then scans all images
  8.  * from that one page. These images are saved to the specified
  9.  * directory.
  10.  */
  11. public class GetImage extends javax.swing.JFrame {
  12.   /**
  13.    * The constructor.
  14.    */
  15.   public GetImage()
  16.   {
  17.     //{{INIT_CONTROLS
  18.     setTitle("Download Images");
  19.     getContentPane().setLayout(null);
  20.     setSize(405,305);
  21.     setVisible(false);
  22.     
  23.     JLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
  24.     JLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
  25.     JLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
  26.     JLabel1.setText("Download images from(one page only):");
  27.     getContentPane().add(JLabel1);
  28.     JLabel1.setBounds(12,12,384,24);
  29.     
  30.     JLabel2.setText("URL:");
  31.     getContentPane().add(JLabel2);
  32.     JLabel2.setBounds(12,36,36,24);
  33.     
  34.     getContentPane().add(_url);
  35.     _url.setBounds(48,36,348,24);
  36.     
  37.     JLabel3.setText("Select local path to download images to:");
  38.     getContentPane().add(JLabel3);
  39.     JLabel3.setBounds(12,72,384,24);
  40.     
  41.     getContentPane().add(_save);
  42.     _save.setBounds(12,96,384,24);
  43.     
  44.     JScrollPane1.setOpaque(true);
  45.     getContentPane().add(JScrollPane1);
  46.     JScrollPane1.setBounds(12,168,384,132);
  47.     JScrollPane1.getViewport().add(_log);
  48.     _log.setBounds(0,0,381,129);
  49.     
  50.     _go.setText("GO!");
  51.     getContentPane().add(_go);
  52.     _go.setBounds(84,132,216,24);
  53.     _go.setActionCommand("jbutton");
  54.     //}}
  55.     //{{INIT_MENUS
  56.     //}}
  57.     //{{REGISTER_LISTENERS
  58.     SymAction lSymAction = new SymAction();
  59.     _go.addActionListener(lSymAction);
  60.     //}}
  61.     setLocation(32,32);
  62.   }
  63.   public void setVisible(boolean b)
  64.   {
  65.     if ( b )
  66.       setLocation(5050);
  67.     super.setVisible(b);
  68.   }
  69.   /**
  70.    * Program entry point.
  71.    *
  72.    * @param args
  73.    */
  74.   static public void main(String args[])
  75.   {
  76.     (new GetImage()).setVisible(true);
  77.   }
  78.   public void addNotify()
  79.   {
  80.     // Record the size of the window prior to calling parents addNotify.
  81.     Dimension size = getSize();
  82.     super.addNotify();
  83.     if ( frameSizeAdjusted )
  84.       return;
  85.     frameSizeAdjusted = true;
  86.     // Adjust size of frame according to the insets and menu bar
  87.     Insets insets = getInsets();
  88.     javax.swing.JMenuBar menuBar = getRootPane().getJMenuBar();
  89.     int menuBarHeight = 0;
  90.     if ( menuBar != null )
  91.       menuBarHeight = menuBar.getPreferredSize().height;
  92.     setSize(insets.left +
  93.             insets.right +
  94.             size.width,
  95.             insets.top +
  96.             insets.bottom +
  97.             size.height + menuBarHeight);
  98.   }
  99.   // Used by addNotify
  100.   boolean frameSizeAdjusted = false;
  101.   //{{DECLARE_CONTROLS
  102.   javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
  103.   javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
  104.   /**
  105.    * The URL to be scanned for images.
  106.    */
  107.   javax.swing.JTextField _url = new javax.swing.JTextField();
  108.   javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
  109.   /**
  110.    * The directory that these images are to be saved in.
  111.    */
  112.   javax.swing.JTextField _save = new javax.swing.JTextField();
  113.   javax.swing.JScrollPane JScrollPane1 = new javax.swing.JScrollPane();
  114.   /**
  115.    * Progress display.
  116.    */
  117.   javax.swing.JList _log = new javax.swing.JList();
  118.   /**
  119.    * Button to be pressed to start the scan.
  120.    */
  121.   javax.swing.JButton _go = new javax.swing.JButton();
  122.   //}}
  123.   //{{DECLARE_MENUS
  124.   //}}
  125.   class SymAction implements java.awt.event.ActionListener {
  126.     public void actionPerformed(java.awt.event.ActionEvent event)
  127.     {
  128.       Object object = event.getSource();
  129.       if ( object == _go )
  130.         Go_actionPerformed(event);
  131.     }
  132.   }
  133.   /**
  134.    * Called for each image. The name(URL) of each image
  135.    * is passed to this method so it can be retrieved and
  136.    * saved.
  137.    *
  138.    * @param name The complete URL to an image to save.
  139.    */
  140.   protected void processImage(String name)
  141.   {
  142.     try {
  143.       if ( _save.getText().length()>0 ) {
  144.         /*
  145.          * 例如:name为http://www.hdu.edu.cn/image/logo_m.gif,
  146.          * 则我们只要把logo_m.gif作为名称加入到路径下面。
  147.          */
  148.         int i = name.lastIndexOf('/');
  149.         if ( i!=-1 ) {
  150.           FileOutputStream fso
  151.           = new FileOutputStream(
  152.                                 new File(_save.getText(),name.substring(i)) );
  153.           /*
  154.            * 不太了解,应该与http.getBody()相似,获得网页图片的字节。
  155.            */
  156.           HTTPSocket http = new HTTPSocket();
  157.           http.send(name,null);
  158.           fso.write( http.getBodyBytes() );
  159.           fso.close();
  160.         }
  161.       }
  162.     } catch ( Exception e ) {
  163.       JOptionPane.showMessageDialog(this,
  164.                                     e,
  165.                                     "Error",
  166.                                     JOptionPane.OK_CANCEL_OPTION,
  167.                                     null );
  168.     }
  169.   }
  170.   /**
  171.    * This is where most of the action takes place. This
  172.    * method is called when the GO! button is pressed.
  173.    *
  174.    * @param event The event
  175.    */
  176.   void Go_actionPerformed(java.awt.event.ActionEvent event)
  177.   {
  178.     try {
  179.       // open the connection and get the page.
  180.       HTTPSocket http = new HTTPSocket();
  181.       HTMLPage page = new HTMLPage(http);
  182.       page.open(_url.getText(),null);
  183.       // look at the images.
  184.       /*
  185.        * 以www.hdu.edu.cn为例:
  186.        * 此时vec为:
  187.        * [http://www.hdu.edu.cn/image/logo_m.gif, 
  188.        * http://www.hdu.edu.cn/image/bannew2.gif, 
  189.        * http://www.hdu.edu.cn/image/daohang1_3.gif, 
  190.        * http://www.hdu.edu.cn/image/daohang1_03.gif, 
  191.        * http://www.hdu.edu.cn/image/daohang_04.gif, 
  192.        * http://www.hdu.edu.cn/image/daohang_05.gif, 
  193.        * http://www.hdu.edu.cn/image/daohang_06.gif, 
  194.        * http://www.hdu.edu.cn/image/daohang_07.gif, 
  195.        * http://www.hdu.edu.cn/image/daohang_08.gif, 
  196.        * http://www.hdu.edu.cn/image/daohang_09.gif, 
  197.        * http://www.hdu.edu.cn/image/daohang_10.gif, 
  198.        * http://www.hdu.edu.cn/image/daohang_11.gif, 
  199.        * http://www.hdu.edu.cn/image/061014.gif, 
  200.        * http://www.hdu.edu.cn/image/announce.gif, 
  201.        * http://www.hdu.edu.cn/image/index_07.gif, 
  202.        * http://www.hdu.edu.cn/image/gg_04.gif, 
  203.        * http://www.hdu.edu.cn/image/redhome.gif, 
  204.        * http://www.hdu.edu.cn/image/tushuguan.gif, 
  205.        * http://www.hdu.edu.cn/image/08jx.gif, 
  206.        * http://www.hdu.edu.cn/image/index_12.gif, 
  207.        * http://www.hdu.edu.cn/image/danxiao.gif, 
  208.        * http://www.hdu.edu.cn/image/meiti.gif, 
  209.        * http://www.hdu.edu.cn/image/zjedu-logo.gif]
  210.        */
  211.       Vector vec = page.getImages();
  212.       if ( vec.size()>0 ) {
  213.         // copy the images to an array for display
  214.         String array[] = new String[vec.size()];
  215.         /*
  216.          * Copies the components of this vector into the 
  217.          * specified array. The item at index k in this 
  218.          * vector is copied into component k of anArray. 
  219.          * The array must be big enough to hold all the 
  220.          * objects in this vector, else an IndexOutOfBoundsException 
  221.          * is thrown.
  222.          */
  223.         vec.copyInto(array);
  224.         /*
  225.          * Constructs a ListModel from an array of 
  226.          * objects and then applies setModel to it. 
  227.          * 构造列表,用于显示所抓到的图片网页地址。
  228.         */
  229.         _log.setListData( array );
  230.         // loop through and process each image
  231.         for ( int i=0;i<vec.size();i++ )
  232.           processImage((String)vec.elementAt(i));
  233.       }
  234.     } catch ( Exception e ) {
  235.       String s[] = new String[1];
  236.       s[0] = "Error: " + e;
  237.       _log.setListData( s );
  238.     }
  239.   }
  240. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值