Results 1 to 5 of 5
  1. #1
    gkaradagan is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Get url image but its not working ...

    Java Code:
    package javaassignment4;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.net.*;
    import java.util.*;
    import java.io.*;
    import java.net.HttpURLConnection;
    import javax.imageio.ImageIO;
    
    public class Main {
         private static String text;
            public static void main(String[] args) {
        System.out.println("Enter the URL address: ");
            Scanner input = new Scanner(System.in);
            String url = input.nextLine();
            String html = getText(url);
    
            /* Output html */
         //   System.out.println(html); 
            
            /********************************
             * Instructions: Get every image path from the html object
             * Concatenate this path with the root domain to form the 
             * "APPROPRIATE" image file path.
             * Pass this  path (url) to URL below
             ********************************/
            
            //Get Image from URL
                
                try{
                    /*
                     * Pass image file paths as parameter
                     * A SAMPLE path is used below
                     */
                    URL imgUrl = new URL("http://nscraps.com/images/logo.png");  //replace with appropriate image path.                
                    Image image = ImageIO.read(imgUrl);
                    BufferedImage cpimg=bufferImage(image);
                    File f1 = new File("image.png"); //Name to save A FILE with.. File saved in current directory
                    ImageIO.write(cpimg, "png", f1);
                }
                
                
                catch(Exception e){
                   System.out.println(e);
              }
    }
    
    public static String getText(String fn) {
    
            // MUCH more efficient than String concatenation.
            // This will hold the HTML file
            StringBuilder text = new StringBuilder();
    
            try {
    
            // fn is the full web address including the http://
            URL page = new URL(fn);
    
            // Connect to the webpage
            HttpURLConnection conn =
            (HttpURLConnection) page.openConnection();
            conn.connect();
    
            // Connect to the CONTENT and set
            // up a BufferedReader for it.
            InputStreamReader in = new InputStreamReader(
            (InputStream) conn.getContent());
            BufferedReader read = new BufferedReader(in);
    
            // Now actually read the HTML
            String line;
            do {
            line = read.readLine();
            if (line != null)
                    text.append(line).append("\n");
            } while (line != null);
    
            } catch (IOException e) {
            return "Error - :" + e.getMessage();
            }
    
            // Changes the StringBuilder to a String
            return text.toString();
          }
    
    /*** 
     * 
     * Below Methods are responsible for copying Image file 
     * 
     ***/
       public static BufferedImage bufferImage(Image image) {
            return bufferImage(image,BufferedImage.TYPE_INT_RGB);
         }
       public static BufferedImage bufferImage(Image image, int type) {
            BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, null, null);
            return bufferedImage;
            }
    }
    when i run this code on netbeans. but netbeans gives me this errors

    Java Code:
    Enter the URL address: 
    http://nscraps.com/images/logo.png
    
    Exception in thread "main" java.lang.ClassCastException: sun.awt.image.URLImageSource cannot be cast to java.io.InputStream
    	at javaassignment4.Main.getText(Main.java:67)
    	at javaassignment4.Main.main(Main.java:17)
    Java Result: 1

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Get url image but its not working ...

    The error message is self explanatory. The Object returned by conn.getContent() is a sun.awt.image.URLImageSource, which can't be cast to java.io.InputStream.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    gkaradagan is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: Get url image but its not working ...

    i change somewhere and i got this result

    run:
    Enter the URL address:
    nscraps.com/images/logo.png
    Error - :no protocol: nscraps.com/images/logo.png
    BUILD SUCCESSFUL (total time: 4 seconds)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Get url image but its not working ...

    Error - :no protocol: nscraps.com/images/logo.png
    The error message says your URL does NOT have a protocol.
    What do you think the protocol is on the URL you are using?

    Do you know what a protocol is?
    Have you read the API doc for the URL class?

Similar Threads

  1. Replies: 1
    Last Post: 08-03-2011, 09:41 AM
  2. Replies: 0
    Last Post: 02-08-2011, 08:51 AM
  3. tiff image in ie7 is not working
    By kailash in forum Java Servlet
    Replies: 1
    Last Post: 02-06-2011, 04:43 PM
  4. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 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
  •