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

    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.util.*;
    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(;
            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
                     * Pass image file paths as parameter
                     * A SAMPLE path is used below
                    URL imgUrl = new URL("");  //replace with appropriate image path.                
                    Image image =;
                    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){
    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();
            // 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)
            } 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:
    Exception in thread "main" java.lang.ClassCastException: sun.awt.image.URLImageSource cannot be cast to
    	at javaassignment4.Main.getText(
    	at javaassignment4.Main.main(
    Java Result: 1

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    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

    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
    Rep Power

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

    i change somewhere and i got this result

    Enter the URL address:
    Error - :no protocol:
    BUILD SUCCESSFUL (total time: 4 seconds)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

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

    Error - :no protocol:
    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, 09:51 AM
  3. tiff image in ie7 is not working
    By kailash in forum Java Servlet
    Replies: 1
    Last Post: 02-06-2011, 05:43 PM
  4. Replies: 2
    Last Post: 01-04-2011, 05: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