您好我使用的是HttpURLConnection
獲取txt
文件的內容,我想知道該文件的大小,我使用內容長度方法,但它返回錯誤的值,例如在此代碼中文件的大小是17509,但它返回5147? so any Help? 非常感謝:)。請求方法GET返回錯誤的內容長度
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();
感謝您的答案http路徑是http://mrsoft.weab.eu/chagvalagar/topic1.txt – Mahfa