2013-10-16 4 views
2

java에서 URL을 여는 방법은 무엇입니까?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로 이동하면 파일을 다운로드합니다. 코드에서이 작업을 구현하는 방법은 무엇입니까?

URL을 열어서 파일을 다운로드하려고합니다.하지만 작동하지 않습니다.

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

내가 뭔가 잘못 알고하지만 난 날엔

+0

도움이되기를 바랍니다. 당신이 시도한 방식대로 작동하지 않았습니까? 자바를 사용하여 URL을 열고 읽는 방법을 알고 싶다면 Google 검색이 도움이 될 것입니다. 다음은 "URL java read"를 쿼리 할 때 반환되는 첫 번째 링크입니다. http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html – Shobit

답변

1

이가 시도 뭐죠 KNW 해달라고 :

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

나는이 발견 :

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; 
    } 
} 

를 사용하려면

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?

나는이 당신에게 당신이 이미 시도했습니다 질문 일에 추가 제안