2013-10-16 79 views
2

如何在開放Java網址是什麼?打開URL在java中

E.g i have https://www.iformbuilder.com/exzact/dataExcelFeed.php? 
PAGE_ID=3175952&TABLE_NAME=_data399173_eff_sor&ANALYTIC=1&SINCE_DATE=2013-10-1 

當我們去這個URL它會下載文件。但如何在代碼中實現這個東西?

我試試這個,打開網址,使文件將downloaded.But它不工作。

URL url = new URL("https://www.iformbuilder.com/exzact/dataExcelFeed.php?PAGE_ID=3175952&TABLE_NAME=_data399173_eff_sor&ANALYTIC=1&SINCE_DATE=2013-10-16"); 
    HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); 
    System.out.println(urlCon); 
    urlCon.connect(); 

我知道什麼是錯的,但我不KNW最新的DAT

+0

我建議增加你已經嘗試過的問題的東西。它是否按照您嘗試的方式工作?如果你只是想知道如何打開和讀取使用Java的URL,谷歌搜索將幫助。這裏所返回的第一個鏈接,當你查詢「URL的java讀」 http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html – Shobit

回答

1

試試這個:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

class HttpHelperx 
{ 
    public static String GET(String url) 
    { 
     String result = ""; 

     try 
     { 
     URL navUrl = new URL(url); 
     URLConnection con = (URLConnection)navUrl.openConnection(); 

     result = getContent(con); 

     } 
     catch (MalformedURLException e) 
     { 
     e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
     e.printStackTrace(); 
     } 

     return result; 
    } 

    public static String getContent(URLConnection con) 
    { 
     String result = ""; 
     if(con!=null) 
     { 
     BufferedReader br; 
     try 
     { 
      br = new BufferedReader(new InputStreamReader(con.getInputStream())); 
      StringBuilder buffer = new StringBuilder(); 
      String aux = ""; 

      while ((aux = br.readLine()) != null) 
      { 
       buffer.append(aux); 
      } 
      result = buffer.toString(); 
      br.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     } 

     return result; 
    } 
} 

要使用它:

public class HTTPHelperDriver 
{ 
    public static void main(String[] args) 
     throws Exception 
    { 
     String response = HttpHelperx.GET("http://www.google.com"); 
     System.out.println(response); 
    } 
} 
2

我發現這一點:

URL website = new URL("http://www.website.com/information.asp"); 
ReadableByteChannel rbc = Channels.newChannel(website.openStream()); 
FileOutputStream fos = new FileOutputStream("information.html"); 
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 

本文內容:How to download and save a file from Internet using Java?

我希望這有助於你