Android检查下载成功

Android检查下载成功,android,http-headers,httpresponse,Android,Http Headers,Httpresponse,对于下载内容,我使用apache类HTTPResponse HTTPClient等。 我检查是否有以下有效下载: entity.writeTo(new FileOutputStream(outfile)); if(outfile.length()!=entity.getContentLength()){ long fileLength = outfile.length(); outfile.delete();

对于下载内容,我使用apache类HTTPResponse HTTPClient等。 我检查是否有以下有效下载:

entity.writeTo(new FileOutputStream(outfile));
        if(outfile.length()!=entity.getContentLength()){
            long fileLength = outfile.length();
            outfile.delete();
            throw new Exception("Incomplete download, "+fileLength+"/"
                    +entity.getContentLength()+" bytes downloaded");

        }

但这一例外似乎从未触发过。如何妥善处理??entity.getContentLength是服务器上文件的长度还是接收的数据量?

文件请求应始终带有MD5校验和。如果您有一个MD5头,那么您需要做的就是对照MD5生成的文件检查它。这样做会更好,因为您可以拥有一个具有相同字节数的文件,但其中一个字节在传输过程中被篡改

        entity.writeTo(new FileOutputStream(outfile));
        String md5 = response.getHeaders("Content-MD5")[0].getValue();
        byte[] b64 = Base64.decode(md5, Base64.DEFAULT);
        String sB64 = IntegrityUtils.toASCII(b64, 0, b64.length);
        if (outfile.exists()) {
            String orgMd5 = null;
            try {
                orgMd5 = IntegrityUtils.getMD5Checksum(outfile);
            } catch (Exception e) {
                    Log.d(TAG,"Exception in file hex...");
            }
            if (orgMd5 != null && orgMd5.equals(sB64)) {
                Log.d(TAG,"MD5 is equal to files MD5");
            } else {
                Log.d(TAG,"MD5 does not equal files MD5");
            }
        }
将此类添加到项目中:

public class IntegrityUtils {
public static String toASCII(byte b[], int start, int length) {
    StringBuffer asciiString = new StringBuffer();

    for (int i = start; i < (length + start); i++) {
        // exclude nulls from the ASCII representation
        if (b[i] != (byte) 0x00) {
            asciiString.append((char) b[i]);
        }
    }

    return asciiString.toString();
}

public static String getMD5Checksum(File file) throws Exception {
    byte[] b = createChecksum(file);
    String result = "";

    for (int i = 0; i < b.length; i++) {
        result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    return result;
}

public static byte[] createChecksum(File file) throws Exception {
    InputStream fis = new FileInputStream(file);

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;

    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);

    fis.close();
    return complete.digest();
}
}
公共类完整性直到{
公共静态字符串toASCII(字节b[],整数开始,整数长度){
StringBuffer ascistring=新的StringBuffer();
对于(int i=开始;i<(长度+开始);i++){
//从ASCII表示中排除空值
如果(b[i]!=(字节)0x00){
ascistring.append((char)b[i]);
}
}
返回ascistring.toString();
}
公共静态字符串getMD5Checksum(文件)引发异常{
字节[]b=创建校验和(文件);
字符串结果=”;
for(int i=0;i0){
完成。更新(缓冲区,0,numRead);
}
}而(numRead!=-1);
fis.close();
返回complete.digest();
}
}

我建议不要使用entity.getContentLength,因为它总是返回-1似乎存在问题,请查看以下内容:。我会远离你的方法,使用更可靠的MD5校验和。遗憾的是,据我所知,很少有服务器发送MD5报头。我要求我的网站管理员将其添加到配置中。还有一些测试指出,在我的例子中,entity.getContentLength返回与response.getHeaders相同的结果(“Content Length”)。我将接受这个答案。