如何在Java中使用HttpResponse處理下載?我向特定站點發出HttpGet請求 - 站點返回要下載的文件。我該如何處理這個下載? InputStream似乎無法處理它(或者我錯誤地使用它)。使用Java處理下載
6
A
回答
8
假設你實際上是在談論HttpClient,這裏是一個SSCCE:
package com.stackoverflow.q2633002;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class Test {
public static void main(String... args) throws IOException {
System.out.println("Connecting...");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://apache.cyberuse.com/httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin.zip");
HttpResponse response = client.execute(get);
InputStream input = null;
OutputStream output = null;
byte[] buffer = new byte[1024];
try {
System.out.println("Downloading file...");
input = response.getEntity().getContent();
output = new FileOutputStream("/tmp/httpcomponents-client-4.0.1-bin.zip");
for (int length; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
System.out.println("File successfully downloaded!");
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
}
}
工作在這裏很好。你的問題在別的地方。
+0
我將內容類型添加到頭(Application/octet-stream)和使用相同的方法,似乎有伎倆。 – Tereno 2010-04-13 23:25:04
0
通常,當您希望瀏覽器顯示要下載的文件的下載對話框時,您應該將傳入的inputstream
內容直接設置爲響應對象steam並將響應的內容類型(對象HttpServletResponse
)設置爲相關的文件類型。
即
response.setContentType(.. relevant content type)
內容類型可以是application/pdf
爲PDF文件,例如,
如果瀏覽器有一個插件在瀏覽器窗口中顯示相關文件,文件將打開,用戶可以保存,否則瀏覽器將顯示下載框。
1
打開一個流併發送文件:
try {
FileInputStream is = new FileInputStream(_backupDirectory + filename);
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[65536];
int numRead;
while ((numRead = is.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, numRead);
}
os.close();
is.close();
}
catch (FileNotFoundException fnfe) {
System.out.println("File " + filename + " not found");
}
相關問題
- 1. 使用WinSCP下載批處理文件
- 2. 如何使用phantomjs/casperjs處理下載?
- 3. 下載邊處理
- 4. 如何處理下載流
- 5. w3wp處理下載文件
- 6. 使用Java處理XSLT?
- 7. 使用java處理JSON Mapreduce
- 8. 處理用java
- 9. 下載使用java的jar
- 10. 從下載使用Java
- 11. 事件在Java中使用通用處理程序處理
- 12. 使用處理程序下載文件後會話被清除
- 13. 使用Perl進行多文件下載和處理
- 14. 使用批處理腳本從聯機URL下載xml
- 15. 如何使用Laravel 5處理從AWS S3下載的圖像?
- 16. 使用多處理器並行下載文件
- 17. 如何使用VB腳本處理IE下載對話框?
- 18. 使用linux從網站下載批處理文件
- 19. 如何使用JavaFX 2.0處理文件下載WebEngine
- 20. Java類加載器安全處理
- 21. 併發下載/處理在C#
- 22. PHP文件下載與圖像處理
- 23. 通過Firefox處理「下載」窗口WebDriver
- 24. 處理非常大的http下載
- 25. 在ASP.NET中處理文件下載
- 26. 在Android中處理下載監聽器
- 27. 下載文件異常處理
- 28. Android:處理多個下載的服務
- 29. 如何處理多個異步下載
- 30. 如何處理ftp下載請求塊
你在說什麼API /庫? [Apache HttpComponents HttpClient v4](http://hc.apache.org/httpcomponents-client/index.html)?如果你不知道,請提及你正在談論的'HttpResponse'和'HttpGet'類的包名,最好發佈一個[SSCCE](http://sscce.org),以便我們發現你的錯誤。 – BalusC 2010-04-13 20:35:30
的確我在使用Apache HttpComponents。你發佈的答案似乎是我正在尋找的。但是,是否有可能將所有輸入存儲爲字符串與實際文件?我的輸入流轉換爲字符串方法使用緩衝讀取器,但它給了我空。 – Tereno 2010-04-13 23:11:57