使用java创建URL [英] Creating URL using java

查看:441
本文介绍了使用java创建URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii to all



我正在尝试使用java创建一个url。地址为 http://www.merchant.com/pg/index.html



当我去在浏览器上运行此地址我找不到错误404页面。我的项目中有一个



index.html文件。



我的代码是



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 is

public 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();
	        }

	}

}



如何解决这个问题? Plz help。


How to solve this issue? Plz help.

推荐答案

我不认为问题出在你的网址定义中。在我检查了你说的网址之后,它实际上有一个HTTP错误404的自定义页面,这意味着找不到资源。但是,网址 http: //www.merchant.com/index.html 是有效的,应该会给你预期的结果。



[说明]

含义是什么;您正在尝试从目标URL中不存在的文件中检索内容。要成功读取URL,必须先在托管服务器的名为pg的目录中创建一个文件。然后在此服务器中,复制文件或创建要访问的文件。否则,您将必须使用有效位置来检索文件内容。



NB 如果您的网址未指向现有网址文件,你会得到一个文件未找到异常...我想你正在堆栈跟踪中得到什么。
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.


这篇关于使用java创建URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆