2012-06-09 37 views
26

我試圖下載使用HttpClient的PDF文件。我能夠拿到文件,但我不知道如何將字節轉換成PDF格式,並在一些地方保存它的系統如何與HttpClient的下載的文件保存到特定的文件夾

我有下面的代碼,我怎麼可以把它存儲爲PDF?

public ???? getFile(String url) throws ClientProtocolException, IOException{ 

      HttpGet httpget = new HttpGet(url); 
      HttpResponse response = httpClient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       long len = entity.getContentLength(); 
       InputStream inputStream = entity.getContent(); 
       // How do I write it? 
      } 

      return null; 
     } 

回答

33
InputStream is = entity.getContent(); 
String filePath = "sample.txt"; 
FileOutputStream fos = new FileOutputStream(new File(filePath)); 
int inByte; 
while((inByte = is.read()) != -1) 
    fos.write(inByte); 
is.close(); 
fos.close(); 

編輯:

你也可以使用BufferedOutputStreamBufferedInputStream更快的下載:

BufferedInputStream bis = new BufferedInputStream(entity.getContent()); 
String filePath = "sample.txt"; 
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); 
int inByte; 
while((inByte = bis.read()) != -1) bos.write(inByte); 
bis.close(); 
bos.close(); 
+6

HttpClient在內部優化了讀取操作。沒有必要用另一種緩衝層 – oleg

+0

我不能直接寫入該文件的路徑,我不得不使用'檔案文件=新的文件(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),「filename.jpg」);'爲寫入外部存儲器,以便其他應用程序可以看到我的應用程序 – Aaron

1

打開FileOutputStream和保存從inputStream給它的字節。

+0

什麼是困惑我的是,你有一個FileOutputStream已經在downloadAndSaveToFile方法打開,但是當我創建的完整路徑的文件對象,我得到一個「文件不存在」錯誤...你可以展示我們的代碼將採取這個文件下載並保存到一個特定的文件夾? –

+0

我很抱歉,我的路上有一個錯字。事實證明,當路徑是有效路徑時,我可以指定具有完整所需路徑的文件對象。 :-) –

21

下面是使用IOUtils.copy()一個簡單的解決方案:

File targetFile = new File("foo.pdf"); 

if (entity != null) { 
    InputStream inputStream = entity.getContent(); 
    OutputStream outputStream = new FileOutputStream(targetFile); 
    IOUtils.copy(inputStream, outputStream); 
    outputStream.close(); 
} 

return targetFile; 

IOUtils.copy()是偉大的,因爲它處理的緩衝。然而,這種解決方案不是很可擴展性:

  • 你不能指定目標文件名和目錄
  • 您可能希望將文件存儲在不同的方式,例如在數據庫中。這種情況下不需要文件。

更具擴展性的解決方案包括兩個功能:

public void downloadFile(String url, OutputStream target) throws ClientProtocolException, IOException{ 
    //... 
    if (entity != null) { 
    //... 
     InputStream inputStream = entity.getContent(); 
     IOUtils.copy(inputStream, target); 
    } 
} 

而且一個輔助方法:

public void downloadAndSaveToFile(String url, File targetFile) { 
    OutputStream outputStream = new FileOutputStream(targetFile); 
    downloadFile(url, outputStream); 
    outputStream.close(); 
} 
+0

這會流式傳輸文件還是將整個文件保存在內存中,然後保存到磁盤? –

26

只是爲了記錄有做同樣的

的更好(容易)的方式
File myFile = new File("mystuff.bin"); 

CloseableHttpClient client = HttpClients.createDefault(); 
try (CloseableHttpResponse response = client.execute(new HttpGet("http://host/stuff"))) { 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     try (FileOutputStream outstream = new FileOutputStream(myFile)) { 
      entity.writeTo(outstream); 
     } 
    } 
} 

或者用流利的API,如果人喜歡它更好

Request.Get("http://host/stuff").execute().saveContent(myFile); 
+2

女巫包做我需要導入的'request.GET中( 「HTTP://主機/東西」)。.execute()saveContent(MYFILE)' – badera

+0

在Maven中你所需要的神器'org.apache.httpcomponents:fluent- hc' - Java包是'org.apache.http.client.fluent.Request' –

相關問題