2012-04-02 19 views
12

Tôi đang tìm kiếm cơ hội mở url trong java.Mở URL bằng Java để lấy nội dung

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

Tôi đã tìm ra cách đó.

Tôi đã thêm nó vào chương trình của mình và xảy ra lỗi sau.

The method openConnection() is undefined for the type URL 

(bởi url.openConnection())

vấn đề của tôi là gì?

tôi sử dụng một tomcat-server với servlets, ...

+0

hãy thử cái này: http://docs.oracle.com/javase/tutor ial/networking/urls/readingURL.html – shem

+0

Vấn đề là * có thể * rằng bạn không sử dụng 'java.net.URL' nhưng một số lớp khác được gọi là 'URL'. –

+1

cảm ơn .... nó hoạt động;) –

Trả lời

2

Bạn có chắc chắn bằng cách sử dụng lớp java.net.URL? Kiểm tra các câu lệnh nhập của bạn.

5

Nó phù hợp với tôi. Vui lòng kiểm tra xem bạn có đang sử dụng đúng cách nhập không?

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

Nó có thể hữu ích hơn để sử dụng một thư viện http client như như this

Có nhiều thứ như access denied, tài liệu chuyển vv để xử lý khi giao dịch với http.

(tuy nhiên, nó là khó xảy ra trong trường hợp này)

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

    } 
} 
8
String url_open ="http://javadl.sun.com/webapps/download/AutoDL?BundleId=76860"; 
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url_open)); 
4

Tiếp theo mã nên làm việc,

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

Nếu bạn chỉ muốn mở ra trang web, tôi nghĩ ít là nhiều hơn trong trường hợp này:

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

     } 
    } 
}