是否可以检查 URLconnection.getInputStream() 的进度?
我想通过 URL 连接检查下载文件的进度。是否可能或者我应该使用另一个库?这是我的 urlconnection 函数:
public static String sendPostRequest(String httpURL, String data) throws UnsupportedEncodingException, MalformedURLException, IOException {
URL url = new URL(httpURL);
URLConnection conn = url.openConnection();
//conn.addRequestProperty("Content-Type", "text/html; charset=iso-8859-2");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
String line, all = "";
while ((line = rd.readLine()) != null) {
all = all + line;
}
wr.close();
rd.close();
return all;
}
我知道整个文件是在这一行(或 Wearg)中下载的?:
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
那么可以在这段代码中执行此操作吗?
I want to check progress of downloading file by URLconnection. Is it possible or should I use another library? This is my urlconnection function:
public static String sendPostRequest(String httpURL, String data) throws UnsupportedEncodingException, MalformedURLException, IOException {
URL url = new URL(httpURL);
URLConnection conn = url.openConnection();
//conn.addRequestProperty("Content-Type", "text/html; charset=iso-8859-2");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
String line, all = "";
while ((line = rd.readLine()) != null) {
all = all + line;
}
wr.close();
rd.close();
return all;
}
I understand that whole file is downloaded in this line (or worng)?:
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
So is it possible to do this in this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需检查响应中是否存在 HTTP
Content-Length
标头即可。更新 根据您的更新,您将
InputStream
包装在BufferedReader
内,并在while
循环内读取。您可以按如下方式计算字节数:+ 2
用于覆盖BufferedReader#readLine()
占用的 CRLF(回车换行)字节。更干净的方法是仅通过InputStream#read(buffer)
读取它,这样您就不需要从字符来回调整字节来计算读取的字节。另请参阅:
java.net.URLConnection
来触发和处理 HTTP 请求?Just check if the HTTP
Content-Length
header is present in the response.Update as per your update, you're wrapping the
InputStream
inside aBufferedReader
and reading inside awhile
loop. You can count the bytes as follows:The
+ 2
is to cover the CRLF (carriage return and linefeed) bytes which are eaten byBufferedReader#readLine()
. More clean approach would be to just read it byInputStream#read(buffer)
so that you don't need to massage the bytes forth and back from characters to calculate the read bytes.See also:
java.net.URLConnection
to fire and handle HTTP requests?将其包装在 javax.swing.ProgressMonitorInputStream 中。但请注意,Java 可能会在开始将整个响应传递到流之前缓冲整个响应......
Wrap it in a javax.swing.ProgressMonitorInputStream. But note that Java may buffer the entire response before it starts delivering it to the stream ...