java 生成url,使用java创建URL

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值