【发布时间】:2014-01-09 16:44:44
【问题描述】:
嗨,我正在使用 HttpURLConnection 获取 txt 文件的内容,我想知道该文件的大小,我使用内容长度方法,但它返回错误值,例如在此代码中文件的大小为17509 但它返回 5147 ?
所以有什么帮助吗?
提前非常感谢:)。
new Thread() {
@Override
public void run() {
String path = parser.getValue(e, "txt");
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u
.openConnection();
c.setRequestMethod("GET");
c.connect();
int lenghtOfFile = c.getContentLength();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
long total = 0;
Log.i("p1",""+lenghtOfFile);
while ((count = in.read(buffer)) != -1) {
total += count;
Log.i("p2",""+total);
bo.write(buffer, 0, count);
}
bo.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
【问题讨论】:
标签: java android http connection