用 Java 处理下载
我如何能够使用 Java 中的 HttpResponse 处理下载?我向特定站点发出了 HttpGet 请求 - 该站点返回要下载的文件。我该如何处理这个下载? InputStream 似乎无法处理它(或者也许我使用它的方式错误。)
How would I be able to handle downloads using HttpResponse in Java? I made an HttpGet request to a specific site - the site returns the file to be downloaded. How can I handle this download? InputStream doesn't seem to be able to handle it (or maybe I'm using it the wrong way.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您实际上正在谈论 HttpClient,这是一个 SSCCE:
在这里工作正常。你的问题出在别的地方。
Assuming you're actually talking about HttpClient, Here's an SSCCE:
Works fine here. Your problem lies somewhere else.
打开流并发送文件:
Open a stream and send the file:
一般来说,当您希望浏览器显示要下载的文件的下载对话框时,您应该将传入的
inputstream
内容直接设置到响应对象 steam 中,并设置响应的内容类型(>HttpServletResponse
对象)到相关的文件类型。即,
以 pdf 文件为例,内容类型可以是
application/pdf
。如果浏览器有插件可以在浏览器窗口中显示相关文件,则文件将打开并且用户可以保存,否则浏览器将显示下载框。
In general when you want the browser to show the download dialog box for a file to be downloaded, you should set the incoming
inputstream
content directly into the response object steam and set the content type of response (HttpServletResponse
object) to the relevant file type.i.e.,
Content type can be
application/pdf
for pdf files as an example.If browser has a plugin to show relevant file in the browser window, file will open and user can save then, otherwise browser will show the download box.