Get url image but its not working ...
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
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
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
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)
Re: Get url image but its not working ...
Quote:
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?