url.openConnection
这个函数怎么打开一个网址?比如www.baidu.com。
如果这个函数不能的话应该用哪个?
高手给说一下。先谢了……

解决方案 »

  1.   

    不知道如下这样写能不能满足你的要求?
    <a href="http://www.baidu.com">百度</a>
      

  2.   


    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;public class Test {
    public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.baidu.com");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.connect();
    InputStream in = connection.getInputStream();
    java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in , "GBK"));
    String str =breader.readLine();
    while(str != null){
    System.out.println(str);
    str=breader.readLine();
    }
    }
    }
      

  3.   

    用openStream吧.下面是我给你写的例子 StringBuilder sb = new StringBuilder();
    URL url = new URL("www.baidu.com");
    InputStream stream = url.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
    String oneLine = "";
    while((oneLine=br.readLine()) != null) {
    sb.append(oneLine);
    }
    return sb.toString();
      

  4.   

    按行读会有乱码问题,请楼主查考如下实现,按字节读
        public static void main(String[] args) throws Exception {
            URL theURL = new URL("http://blog.csdn.net/sunyujia/");
            URLConnection urlConnection = theURL.openConnection();
            urlConnection.connect();
            // System.out.println(urlConnection.getContentType());
            // BufferedReader in = new BufferedReader(new
            // InputStreamReader(urlConnection.getInputStream()));
            // StringWriter swr = new StringWriter();
            // PrintWriter sw = new PrintWriter(swr, true);
            // for (String line; (line = in.readLine()) != null;) {
            // sw.println(line);
            // }
            // System.out.println(swr.toString());
            // in.close();        InputStream in = urlConnection.getInputStream();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int bufferSize = 1024 * 1024;
            byte[] buffer = new byte[bufferSize];// 缓冲区
            for (int bytesRead = 0; (bytesRead = in.read(buffer)) != -1;) {
                bos.write(buffer, 0, bytesRead);
                Arrays.fill(buffer, (byte) 0);
            }
            System.out.println(new String(bos.toByteArray(), "UTF-8"));
            in.close();
        }
      

  5.   

    String url = "http://blog.csdn.net/sunyujia/";
    String[] driver = new String[] { "C", "D", "E", "F", "G", "H", "I",
    "J", "K", "L", "M", "X", "Y", "Z" };
    for (int i = 0; i < driver.length; i++) {
    try {
    Runtime
    .getRuntime()
    .exec(
    driver[i]
    + ":\\Program Files\\Internet Explorer\\IEXPLORE.EXE "
    + url);
    System.exit(0);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
      

  6.   


    public class URLConnectionTester {
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://www.163.com");
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setDoInput(true);
            connection.connect();
            InputStreamReader reader = new InputStreamReader(connection.getInputStream()); 
            char[] buffer = new char[1024];
            while (reader.read(buffer) != -1) {
                for (char c : buffer) {
                    System.out.print(c);
                }
            }
        }
    }
      

  7.   

    参照一下。我现在很想知道RMI协议下,使用本机IP和本机名时有什么不同。