服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java url编程


import java.io.*;
import java.net.*;
//
//
// gethost.java
//
//
public class gethost 
{
public static void main (string arg[]){
if (arg.length>=1){
inetaddress[] inet;
int i=1;
try{
for (i=1;i<=arg.length;i++){
inet = inetaddress.getallbyname(arg[i-1]);
for (int j=1;j<=inet.length;j++){
system.out.print(inet[j-1].tostring());
system.out.print("");
}
}
}
catch(unknownhostexception e){
system.out.print("unknown hostname!"+arg[i-1]);
}
}
 else{
system.out.print("usage java/jview getip <hostname>");
 }
}
}
</pre>
</p>
<p>
<pre>
<font color=red>example 2</font>
<a href="gethtml.java">download now</a>
//gethtml.java
/**
 * this is a program which can read information from a web server.
 * @version 1.0 2000/01/01
 * @author jdeveloper
**/
import java.net.*;
import java.io.*;

public class gethtml {
public static void main(string args[]){
if (args.length < 1){
system.out.println("usage: java gethtml httpaddress");
system.exit(1);
}
string surladdress = new string(args[0]);
url    url = null;
try{
   url = new url(surladdress);
}catch(malformedurlexception e){
   system.err.println(e.tostring());
                   system.exit(1);
}
try{
                   inputstream ins = url.openstream();
   bufferedreader breader = new bufferedreader(new inputstreamreader(ins));
                   string info = breader.readline();  
                   while(info != null){
                        system.out.println(info);
                        info  = breader.readline();  
   }   
}
                catch(ioexception e){
   system.err.println(e.tostring());
                   system.exit(1);
}
}
}

扫描关注微信公众号