Open URL in Java to get the content

2019-02-08 08:14发布

I´m searching for a opportunity to open a url in java.

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

I found that way.

I added it in my program and the following error occurred.

The method openConnection() is undefined for the type URL

(by url.openConnection())

What is my problem?

I use a tomcat-server with servlets, ...

8条回答
Lonely孤独者°
2楼-- · 2019-02-08 08:43

It works for me. Please check if you are using the right imports?

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
查看更多
Root(大扎)
3楼-- · 2019-02-08 08:44
String url_open ="http://javadl.sun.com/webapps/download/AutoDL?BundleId=76860";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url_open));
查看更多
登录 后发表回答