Help please..Java net


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help please..Java net

  1. #1
    Join Date
    Jul 2006
    Posts
    6

    Help please..Java net

    Hi All,

    Kindly let me know how to down load an image in a web page to an Image
    Object by a Java application .Example " logo.gif" of this page.

    Many thanks in advance
    ben1967

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Hope I got this right..

    Here is an application that requires you to enter the image url. You find that by right clicking the image ->properties. The key class is the SiteConn class.
    This class can also download the entire web page (getContents()), then you may use the Http parser utilities in java to disect the page completely
    Code:
    import javax.swing.*;
    
    /**
     * Get an image from the net
     */
    
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
     * Application frame
     */
    public class ImageGrabber extends JFrame implements ActionListener {
      BorderLayout borderLayout1 = new BorderLayout();
      ImagePan imgPan = new ImagePan();
      JPanel jPanel2 = new JPanel();
      BorderLayout borderLayout2 = new BorderLayout();
      JButton getBtn = new JButton();
      JTextField urlTF = new JTextField();
    
      public ImageGrabber() {
        try {
          jbInit();
          getBtn.addActionListener(this); // hook up button
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        ImageGrabber imageGrabber = new ImageGrabber();
        imageGrabber.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        imageGrabber.setBounds(20,20, 500,400);
        imageGrabber.setVisible(true);
      }
      /**
       * Initialize the application
       */
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jPanel2.setLayout(borderLayout2);
        getBtn.setText("Get Image");
        urlTF.setText("http://");
        this.getContentPane().add(imgPan, BorderLayout.CENTER);
        this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
        jPanel2.add(getBtn, BorderLayout.WEST);
        jPanel2.add(urlTF, BorderLayout.CENTER);
      }
      /**
       * Get url string, retrieve image bytes, create image and display it
       * @param e
       */
      public void actionPerformed(ActionEvent e) {
        try {
          String urlStr = urlTF.getText().trim();
          URL url = new URL(urlStr);
          SiteConn aConnection = new SiteConn(url);
          byte[] b = aConnection.getByteBuffer();
          Image img = Toolkit.getDefaultToolkit().createImage(b);
          imgPan.setImage(img);
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
    /* a very basic image display panel */
    class ImagePan extends JPanel {
      private Image img=null;
    
      public void setImage(Image img) {
        this.img=img;
        repaint();
      }
      public void paintComponent(Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.white);
        g.fillRect(0,0,getWidth(), getHeight());
        if (img==null) {
          g.setColor(Color.black);
          g.drawString("No image loaded yet", 20, 20);
        } else {
          g.drawImage(img, 0,0,this);
        }
        g.setColor(c);
      }
    }
    
    /** SITECONN
    Usage example:
    try {
      URL url=new URL("http://www.whatever.com/servlets/store?text=storeThisTExt");
      SiteConn aConnection=new SiteConn(url);
      StringBuffer sb=aConnection.getContens();
      System.out.println(sb.toString());
    } catch (MalFormedURLException mfe) {
      System.err.println(mfe.getMessage());
    }
     *
     */
    
    class SiteConn {
      URL url=null;
      public SiteConn(URL  url) {
        this.url=url;
      }
      public StringBuffer getContents() throws Exception {
        StringBuffer buffer;
        String line;
        int responseCode;
        HttpURLConnection connection;
    
        BufferedReader dataInput;
        connection = (HttpURLConnection)url.openConnection();
        responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
        try {
          buffer = new StringBuffer();
          InputStream input = connection.getInputStream();
          dataInput = new BufferedReader(new InputStreamReader(input));
    
          while ( (line = dataInput.readLine()) != null) {
            buffer.append(line);
            buffer.append('\n');
            //System.out.println("line: " + line);
          }
          input.close();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
       return buffer;
      }
      public byte [] getByteBuffer () throws Exception {
    
        String line;
        int responseCode;
        HttpURLConnection connection;
    
    
        connection = (HttpURLConnection)url.openConnection();
        InputStream input=connection.getInputStream();
    
        responseCode = connection.getResponseCode();
    
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
    
        try {
          byte [] b=new byte[1420];
          BufferedInputStream in=new BufferedInputStream(input);
          ByteArrayOutputStream out=new ByteArrayOutputStream();
          //System.out.println("starting");
          int n=in.read(b);
          //System.out.println("start:"+n);
          while (n  > 0) {
            out.write(b,0,n);
            n=in.read(b);
            //System.out.println("buf:"+n);
          }
          out.close();
          input.close();
          return out.toByteArray();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
    
      }
    
    }
    Last edited by sjalle; 07-16-2006 at 11:38 AM.
    eschew obfuscation

  3. #3
    Join Date
    Jul 2006
    Posts
    6
    Quote Originally Posted by sjalle
    Here is an application that requires you to enter the image url. You find that by right clicking the image ->properties. The key class is the SiteConn class.
    This class can also download the entire web page (getContents()), then you may use the Http parser utilities in java to disect the page completely
    Code:
    import javax.swing.*;
    
    /**
     * Get an image from the net
     */
    
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
     * Application frame
     */
    public class ImageGrabber extends JFrame implements ActionListener {
      BorderLayout borderLayout1 = new BorderLayout();
      ImagePan imgPan = new ImagePan();
      JPanel jPanel2 = new JPanel();
      BorderLayout borderLayout2 = new BorderLayout();
      JButton getBtn = new JButton();
      JTextField urlTF = new JTextField();
    
      public ImageGrabber() {
        try {
          jbInit();
          getBtn.addActionListener(this); // hook up button
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        ImageGrabber imageGrabber = new ImageGrabber();
        imageGrabber.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        imageGrabber.setBounds(20,20, 500,400);
        imageGrabber.setVisible(true);
      }
      /**
       * Initialize the application
       */
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jPanel2.setLayout(borderLayout2);
        getBtn.setText("Get Image");
        urlTF.setText("http://");
        this.getContentPane().add(imgPan, BorderLayout.CENTER);
        this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
        jPanel2.add(getBtn, BorderLayout.WEST);
        jPanel2.add(urlTF, BorderLayout.CENTER);
      }
      /**
       * Get url string, retrieve image bytes, create image and display it
       * @param e
       */
      public void actionPerformed(ActionEvent e) {
        try {
          String urlStr = urlTF.getText().trim();
          URL url = new URL(urlStr);
          SiteConn aConnection = new SiteConn(url);
          byte[] b = aConnection.getByteBuffer();
          Image img = Toolkit.getDefaultToolkit().createImage(b);
          imgPan.setImage(img);
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
    /* a very basic image display panel */
    class ImagePan extends JPanel {
      private Image img=null;
    
      public void setImage(Image img) {
        this.img=img;
        repaint();
      }
      public void paintComponent(Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.white);
        g.fillRect(0,0,getWidth(), getHeight());
        if (img==null) {
          g.setColor(Color.black);
          g.drawString("No image loaded yet", 20, 20);
        } else {
          g.drawImage(img, 0,0,this);
        }
        g.setColor(c);
      }
    }
    
    /** SITECONN
    Usage example:
    try {
      URL url=new URL("http://www.whatever.com/servlets/store?text=storeThisTExt");
      SiteConn aConnection=new SiteConn(url);
      StringBuffer sb=aConnection.getContens();
      System.out.println(sb.toString());
    } catch (MalFormedURLException mfe) {
      System.err.println(mfe.getMessage());
    }
     *
     */
    
    class SiteConn {
      URL url=null;
      public SiteConn(URL  url) {
        this.url=url;
      }
      public StringBuffer getContents() throws Exception {
        StringBuffer buffer;
        String line;
        int responseCode;
        HttpURLConnection connection;
    
        BufferedReader dataInput;
        connection = (HttpURLConnection)url.openConnection();
        responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
        try {
          buffer = new StringBuffer();
          InputStream input = connection.getInputStream();
          dataInput = new BufferedReader(new InputStreamReader(input));
    
          while ( (line = dataInput.readLine()) != null) {
            buffer.append(line);
            buffer.append('\n');
            //System.out.println("line: " + line);
          }
          input.close();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
       return buffer;
      }
      public byte [] getByteBuffer () throws Exception {
    
        String line;
        int responseCode;
        HttpURLConnection connection;
    
    
        connection = (HttpURLConnection)url.openConnection();
        InputStream input=connection.getInputStream();
    
        responseCode = connection.getResponseCode();
    
        if (responseCode != HttpURLConnection.HTTP_OK) {
          throw new Exception("HTTP response code: " +
             String.valueOf(responseCode));
        }
    
        try {
          byte [] b=new byte[1420];
          BufferedInputStream in=new BufferedInputStream(input);
          ByteArrayOutputStream out=new ByteArrayOutputStream();
          //System.out.println("starting");
          int n=in.read(b);
          //System.out.println("start:"+n);
          while (n  > 0) {
            out.write(b,0,n);
            n=in.read(b);
            //System.out.println("buf:"+n);
          }
          out.close();
          input.close();
          return out.toByteArray();
        } catch (Exception ex) {
          ex.printStackTrace(System.err);
          return null;
        }
    
      }
    
    }
    Dear sjalle,

    Thank you .
    This code serves my purpose. Infact you gave me more than what I wanted.

    ben1967

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  2. .NOT vs .NET
    By Robert Lantry in forum .NET
    Replies: 88
    Last Post: 04-05-2002, 08:03 AM
  3. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center