在俺机子上连http://www.sina.com.cn都不可用,连不上???
只能连上自己建的服务器,
yumen O!!!

解决方案 »

  1.   

    这个是我正在使用的代码,也是有同样的问题。
    就是读取那个网站的数据不行,别的网站都可以。package com.redv.projects.bittorrent;import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.*;
    /**
     *
     * <p>Title: 配合解析html文件以插入到数据库</p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    public class Net {
      public Net() {
      }  public void writeToFile(File file) throws MalformedURLException,
          FileNotFoundException, IOException {
        System.out.println("Writing file " + file);
        try {
          int b;
          FileOutputStream fos = null;
          fos = new FileOutputStream(file);
          DataInputStream dis = new DataInputStream(myURL.openStream());
          String inputLine;
          while ( (b = dis.read()) != -1) {
            fos.write(b);
          }
          dis.close();
          fos.close();
        }
        catch (MalformedURLException me) {
          throw new MalformedURLException(me.getMessage());
        }
        catch (FileNotFoundException ex) {
          throw new FileNotFoundException(ex.getMessage());
        }
        catch (IOException ex) {
          throw new IOException(ex.getMessage());
        }
      }  /**
       * 从Internet等到网页的源文件
       * @return String:source file content
       */
      public String getHTML() {
        String HTML = new String("");
        try {
          DataInputStream dis = new DataInputStream(myURL.openStream());
          String inputLine;
          while ( (inputLine = dis.readLine()) != null) {
            HTML = HTML.concat(inputLine);
            HTML = HTML.concat("\n");
          }
          dis.close();
        }
        catch (MalformedURLException me) {
          System.out.println(me);
        }
        catch (Exception e) {
          System.out.println(e);
        }
        String ret = HTML;
        try {
          ret = new String(HTML.getBytes("ISO-8859-1"), "GBK");
        }
        catch (java.io.UnsupportedEncodingException ex) {}
        return ret;
      }  /**
       * 设置URL
       * @param myURL URL
       */
      public void setURL(URL myURL) {
        this.myURL = myURL;
      }  public void setURL(String url) throws java.net.MalformedURLException {
        this.myURL = new URL(url);
      }  public URL getURL() {
        return this.myURL;
      }  private URL myURL; //The Internet URL  public static void main(String[] args) throws Exception{
        Net net = new Net();
        net.setURL("http://www.cumt.edu.cn");
        net.writeToFile(new File("C:\\mytest.html"));//正常的输出
        System.out.println(net.getHTML());
        net.setURL("http://52bt.vicp.net:800/bt/");
        net.writeToFile(new File("C:\\mytest2.html"));//不正常的输出
        System.out.println(net.getHTML());
      }}