1

我在 Internet 上有一个文件的 url。我需要计算 SHA1 哈希,并按每一行读取这个文件。我知道怎么做,但是我读了两次这个文件,这可能不是一个很好的解决方案。

我怎样才能更有效地做到这一点?

这是我的代码:

URL url = new URL(url);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(1000);
urlConnection.setReadTimeout(1000);
logger.error(urlConnection.getContent() + " ");
InputStream is = urlConnection.getInputStream();


// first reading of file is:

int i;
File file = new File("nameOfFile");
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = 
           new BufferedOutputStream(new FileOutputStream(file.getName()));
while ((i = bis.read()) != -1) {
    bos.write(i);
}
bos.flush();
bis.close();   
sha1(file);

// second reading of file is:

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;

while ((line = reader.readLine()) != null) {
   // do something
}

protected byte[] sha1(final File file) throws Exception {
    if (file == null || !file.exists()) {
        return null;
    }
    final MessageDigest messageDigest = MessageDigest.getInstance(SHA1);

    InputStream is = new BufferedInputStream(new FileInputStream(file));
    try {
        final byte[] buffer = new byte[1024];
        for (int read = 0; (read = is.read(buffer)) != -1;) {
            messageDigest.update(buffer, 0, read);
        }
    } finally {
        IOUtils.closeQuietly(is);
    }
    return messageDigest.digest();
}
4

1 回答 1

4

如果您通过 a 传递它DigestInputStream,它将执行MessageDigest并且仍然可以用作InputStream.

DigestInputStream dis = new DigestInputStream(is,
  MessageDigest.getInstance(SHA1));
BufferedInputStream bis = new BufferedInputStream(dis);
BufferedOutputStream bos = new BufferedOutputStream(
  new FileOutputStream(file.getName()));
while ((i = bis.read()) != -1) {
   bos.write(i);
}
bos.close();
return dis.getMessageDigest().digest();
于 2013-07-18T17:13:31.513 回答