2012-04-23 71 views
0

某些PHP站點使用頁面充當處理文件下載的中間人。以編程方式下載通過PHP頁面推送的文件

使用瀏覽器可以透明地工作。在php頁面處理請求時,似乎有一點暫停。

但是,嘗試使用URLHttpURLConnection通過Java下載將返回一個純html頁面。我怎樣才能以相同的方式使文件下載工作?

編輯:下面是一個例子鏈接:

http://depot.eice.be/index.php?annee_g=jour&cours=poo

編輯:下面是一些我一直在測試代碼:

// This returns an HTML page 

private void downloadURL(String theURL) { 
    URL url; 
    InputStream is = null; 
    DataInputStream dis; 
    String s; 
    StringBuffer sb = new StringBuffer(); 

    try { 
     url = new URL(theURL); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     conn.setRequestMethod("GET"); 
     conn.connect(); 

     if (conn.getResponseCode()!=HttpURLConnection.HTTP_OK) 
      return; 


     InputStream in = conn.getInputStream(); 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     int i; 
     while ((i = in.read()) != -1) { 
      bos.write(i); 
     } 

     byte[] b = bos.toByteArray(); 

     FileOutputStream fos = new FileOutputStream(getNameFromUrl(theURL)); 
     fos.write(b); 
     fos.close(); 
     conn.disconnect(); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

// This will throw Exceptions if the URL isn't in the expected format 

public String getNameFromUrl(String url) { 

    int slashIndex = url.lastIndexOf('/'); 
    int dotIndex = url.lastIndexOf('.'); 

    System.out.println("url:" + url + "," + slashIndex + "," + dotIndex); 

    if (dotIndex == -1) { 
     return url.substring(slashIndex + 1); 
    } else { 
     try { 
      return url.substring(slashIndex + 1, url.length()); 
     } catch (StringIndexOutOfBoundsException e) { 
      return ""; 

     } 
    } 
} 
+0

沒有足夠的信息。該頁面是否需要一些認證?它使用cookie嗎?你嘗試遵循重定向嗎? – 2012-04-23 19:05:21

+0

@EugeneRetunsky沒有身份驗證,沒有cookie。鏈接公開了一個php頁面(例如:download.php?f = ...),它充當中間人。我想知道的是如何在瀏覽器遇到這樣的鏈接時重現瀏覽器的行爲。 – 2012-04-25 10:13:23

回答

0

我想我已經找到了使用HttpUnit的一個解決方案。如果您希望看到如何處理,框架的來源可用。

public void downloadURL(String url) throws IOException { 

    WebConversation wc = new WebConversation(); 
    WebResponse indexResp = wc.getResource(new GetMethodWebRequest(url)); 
    WebLink[] links = new WebLink[1]; 
    try { 
     links = indexResp.getLinks(); 
    } catch (SAXException ex) { 
     // Log 
    } 

    for (WebLink link : links) { 
     try { 
      link.click(); 
     } catch (SAXException ex) { 
      // Log 
     } 
     WebResponse resp = wc.getCurrentPage(); 
     String fileName = resp.getURL().getFile(); 
     fileName = fileName.substring(fileName.lastIndexOf("/") + 1); 
     System.out.println("filename:" + fileName); 
     File file = new File(fileName); 
     BufferedInputStream bis = new BufferedInputStream(
       resp.getInputStream()); 
     BufferedOutputStream bos = new BufferedOutputStream(
       new FileOutputStream(file.getName())); 
     int i; 
     while ((i = bis.read()) != -1) { 
      bos.write(i); 
     } 
     bis.close(); 
     bos.close(); 
    } 
    System.out.println("Done downloading."); 
} 
2

不考慮其他限制,您可以從HTTP標頭中讀取重定向的URL,並直接從JAV連接到該URL一個。

2

有一個API設置可以自動執行重定向 - 但默認情況下應該是true。你如何訪問URL?

Java API docs ...

+0

我已經爲你發佈了一些代碼。我會看看如果使用其中的一種方法會有所作爲。我的印象是,瀏覽器發生的事情是它在PHP頁面繁忙時暫停一段時間,而Java應用程序馬上開始下載。 – 2012-04-25 10:47:38

+0

任何HTTP請求都會有它自己的超時...請求的連接...被處理。如果服務器及時響應,則應該相應地處理重定向的URL。 – 2012-04-25 17:19:09

+0

我明白了。也許在PHP方面發生了一些導致差異的事情。查看爲某些工作代碼添加的答案。這將從上面的示例url獲得預期的文件或頁面。 – 2012-04-27 08:12:33

相關問題