【发布时间】:2011-03-01 23:06:20
【问题描述】:
我想通过 URLconnection 检查下载文件的进度。有可能还是我应该使用另一个库?这是我的 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;
}
我知道整个文件是在这一行下载的(或穿的)?:
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "ISO-8859-2"));
那么可以在这段代码中做到这一点吗?
【问题讨论】:
-
我发布了一个类,它使用
ChangeListener报告来自InputStream的读取字节。它是透明的并且独立于编码。 stackoverflow.com/a/10071341/893578
标签: java progress urlconnection