【问题标题】:Open URL in Java to get the content在 Java 中打开 URL 以获取内容
【发布时间】:2012-04-16 03:50:35
【问题描述】:

我正在寻找在 java 中打开 url 的机会。

URL url = new URL("http://maps.google.at/maps?saddr=4714&daddr=Marchtrenk&hl=de");
    InputStream is = url.openConnection().getInputStream();

    BufferedReader reader = new BufferedReader( new InputStreamReader( is )  );

    String line = null;
    while( ( line = reader.readLine() ) != null )  {
       System.out.println(line);
    }
    reader.close();

我是这样找到的。

我在程序中添加了它,出现以下错误。

The method openConnection() is undefined for the type URL

(通过 url.openConnection())

我的问题是什么?

我使用带有 servlet 的 tomcat 服务器,...

【问题讨论】:

标签: java url inputstream openurl


【解决方案1】:

我在谷歌搜索时发现了这个问题。请注意,如果您只想通过字符串之类的方式使用 URI 的内容,请考虑使用 Apache 的 IOUtils.toString() 方法。

例如,示例代码行可以是:

String pageContent = IOUtils.toString("http://maps.google.at/maps?saddr=4714&daddr=Marchtrenk&hl=de", Charset.UTF_8);

【讨论】:

    【解决方案2】:

    如果你只是想打开网页,我觉得这种情况下少即是多:

    import java.awt.Desktop;
    import java.net.URI; //Note this is URI, not URL
    
    class BrowseURL{
        public static void main(String args[]) throws Exception{
            // Create Desktop object
            Desktop d=Desktop.getDesktop();
    
            // Browse a URL, say google.com
            d.browse(new URI("http://google.com"));
    
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      以下代码应该可以工作,

      URL url = new URL("http://maps.google.at/maps?saddr=4714&daddr=Marchtrenk&hl=de");
      InputStream is = url.openConnection().getInputStream();
      
      BufferedReader reader = new BufferedReader( new InputStreamReader( is )  );
      
      String line = null;
      while( ( line = reader.readLine() ) != null )  {
         System.out.println(line);
      }
      reader.close();
      

      【讨论】:

        【解决方案4】:
        String url_open ="http://javadl.sun.com/webapps/download/AutoDL?BundleId=76860";
        java.awt.Desktop.getDesktop().browse(java.net.URI.create(url_open));
        

        【讨论】:

          【解决方案5】:
          public class UrlContent{
              public static void main(String[] args) {
          
                  URL url;
          
                  try {
                      // get URL content
          
                      String a="http://localhost:8080/TestWeb/index.jsp";
                      url = new URL(a);
                      URLConnection conn = url.openConnection();
          
                      // open the stream and put it into BufferedReader
                      BufferedReader br = new BufferedReader(
                                         new InputStreamReader(conn.getInputStream()));
          
                      String inputLine;
                      while ((inputLine = br.readLine()) != null) {
                              System.out.println(inputLine);
                      }
                      br.close();
          
                      System.out.println("Done");
          
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
          
              }
          }
          

          【讨论】:

            【解决方案6】:

            使用像this这样的http客户端库可能更有用

            在处理 http 时,还有更多事情需要处理,例如拒绝访问、移动文档等。

            (不过,在这种情况下不太可能)

            【讨论】:

              【解决方案7】:

              它对我有用。 请检查您是否使用了正确的导入?

              import java.io.BufferedReader;
              import java.io.InputStream;
              import java.io.InputStreamReader;
              import java.net.URL;
              

              【讨论】:

                【解决方案8】:

                您确定使用java.net.URL 类吗?检查您的导入语句。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-07-08
                  • 2011-08-30
                  • 2012-08-20
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-06-14
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多