Hii to all
I m trying to create a url using java. Address is http://www.merchant.com/pg/index.html
When i m going to run this address on browser i got error 404 page not found. There is a
index.html file in my project.
My code ispublic class Url {
/**
* @param args
*/
public static void main(String[] args) {
try {
//
// Creating a url object by specifing each parameter separately, including
// the protocol, hostname, port number, and the page name
//
URL url = new URL("http", "www.Merchant.com" , 80, "/pg/index.html");
//URL url = new URL("http://Merchant.com/index.html");
// We can also specify the address in a single line.
//www.merchant.com/pg/index.html
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
How to solve this issue? Plz help.
解决方案
I don't think the problem is in your URL definition. After I checked the url you said, it in fact has a customized page for the HTTP Error 404, which means resource not found. However, the url http://www.merchant.com/index.html is valid and should give you the expected result.
[Explanation]
What it means is; You are trying to retrieve content from a file that does not exist in the destination URL. In order to read the URL successfully, you must first create a file in the directory named pg of your hosting server. Then in this server, copy your file or create a file that you want to be accessible. Otherwise, you will have to use a valid location to retrieve the file content.
N.B. If you the url does not point to an existing file, you will get a file not found exception...which I suppose is what you're getting in your stack trace.