Java downloader从已签名的exe中删除数字证书

Java downloader从已签名的exe中删除数字证书,java,digital-certificate,Java,Digital Certificate,我正在使用java downloader,它从远程服务器提取文件并将其保存在本地 以下是我使用的代码: try { BufferedInputStream in = new java.io.BufferedInputStream(new URL(link).openStream()); FileOutputStream fos = new FileOutputStream(destination); BufferedOutputStream bout = new Buf

我正在使用java downloader,它从远程服务器提取文件并将其保存在本地

以下是我使用的代码:

try {

    BufferedInputStream in = new java.io.BufferedInputStream(new URL(link).openStream());
    FileOutputStream fos = new FileOutputStream(destination);
    BufferedOutputStream bout = new BufferedOutputStream(fos,1024);

    byte data[] = new byte[1024];
    int count;

        while( (count = in.read(data,0,1024)) != -1){
        bout.write(data,0,count);
        }

    fos.flush();
    fos.close();
    fos.close();


    } catch(Throwable t){
    t.printStackTrace();
} 
我注意到的第一件事是,服务器上的文件和下载的文件大小不完全相同-->(下载的文件小了几个字节)。请注意,“磁盘上的大小”看起来是相同的

我注意到的下一件事是,如果我尝试以管理员身份运行java下载的.exe,它会显示“Publisher:Unknown”,这是不正确的,因为.exe是数字签名的。“数字签名”选项卡均不存在

有什么线索吗


p.S我用Hex editor打开了这两个文件。它们似乎都有相同的内容,只是java下载的文件缺少最后94个字节。

您正在写入一个
缓冲输出流,但正在刷新一个下游流。换句话说,您在错误的流上调用了
flush()

bufferedOutput流刷新为

try {
  BufferedInputStream in = new java.io.BufferedInputStream(new URL(link).openStream());
  FileOutputStream fos = new FileOutputStream(destination);
  BufferedOutputStream bout = new BufferedOutputStream(fos,1024);

  byte data[] = new byte[1024];
  int count;

  while( (count = in.read(data,0,1024)) != -1){
    bout.write(data,0,count);
  }

  bout.flush();
  bout.close();

  in.close();

} catch(Throwable t){
  t.printStackTrace();
} 
您还应该
关闭
缓冲输出流
,它将自动将关闭的消息传播到底层流。换句话说,您不会直接关闭
FileOutputStream

最后,实际上不需要在
close()
之前调用
flush()
,因为执行
close()
将自动执行
flush()

另外,如果使用块,可以让编译器为您插入闭包