使用Java

时间:2016-05-23 13:39:11

标签: java file stream binary

所以我有这个代码从磁盘(tar.gz,exe或dmg)读取正确的安装程序文件,并将其流式传输给用户(下面的代码)。安装程序实际上是存档,可以解压缩并设置可以手动运行(这适用于Windows,需要安装Mac安装程序,还需要提取Unix安装程序)

    InputStream in = null;
    OutputStream out = null;

    byte[] buffer = new byte[16384];

    try {
        String bundle = ServletRequestUtils.getRequiredStringParameter(request, "bundle");

        String installerPath = constructFilePath(bundle);
        File installer = new File(installerPath);
        if(!installer.exists()){
            logger.error("Cannot read installer file");
            response.sendRedirect("/somewhere");
        }else{
            in = new FileInputStream(installer);
            response.setContentType(getBundleContentType(bundle)); //application/octet-stream or application/x-gzip or application/x-apple-diskimage
            response.setHeader("Pragma", "private"); 
            response.setHeader("Cache-Control", "private, must-revalidate"); 
            response.addHeader("Content-Disposition", "attachment;filename="+getBundleFileName(bundle)); //Setting new file name
            out = new BufferedOutputStream(response.getOutputStream());

            while((in.read(buffer)) != -1)
                out.write(buffer);
        }
    } catch (Exception e) {
        logger.error("Exception downloading installer file, reason: " + e);
        response.sendRedirect("/somewhere");
    } finally {
        if(in != null){
            in.close();
        }
        if(out != null){
            out.flush();
            out.close();
        }
    }

    return null;

我将以Windows(.exe)安装程序为例。以前,当我将代码重定向到http:///somepath/installer.exe进行下载时,该文件将被下载并且我能够使用7zip提取它,但现在,当我尝试提取它时用7zip,我得到了:

Cannot open file as archive.

但是,我可以双击.exe并成功完成安装。我也可以使用winRAR来提取它。

Unix安装程序也发生了同样的事情。当我将它下载到Unix机器并尝试解压缩时(通过右键单击“Extract here”)我收到此错误:

gzip: stdin: decompression OK, trailing garbage ignored 
/bin/gtar: Child returned status 2 
/bin/gtar: Error is not recoverable: exiting now

但是,我可以用“ark”打开它并正确提取其内容。

我还应该指出,下载后文件的字节数不匹配(下载的文件与文件系统上的文件系统相比,应该是相同的。)

我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

您可以尝试写下您读取的完全相同的数据:

while ((read = in.read(buffer)) != -1) {
    out.write(buffer, 0, read);
}

答案 1 :(得分:0)

这是因为你正在编写整个缓冲区。

想象一下这个文件是16385字节。

第一个in.read(buffer)将填满整个缓冲区并返回16384.然后,您将编写整个缓冲区。第二次,它将读取一个字节,然后再次写入整个缓冲区。

有人打败了我,但我想补充说你可以用IOUtils来做这件事......

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/IOUtils.html

例如IOUtils.copy(in, out)

相关问题