BufferedOutputStream 写入垃圾数据

发布于 2024-11-16 23:05:40 字数 764 浏览 1 评论 0原文

我正在编写下载 servlet,它读取 html 文件并写入 servletOutputStream,传输的文件末尾的问题是添加一些垃圾数据,对此有任何建议,

下面是我为此使用的代码


        int BUFFER_SIZE = 1024 * 8;
        servOut   = response.getOutputStream();
        bos       = new BufferedOutputStream(servOut);
        fileObj = new File(file);
        fileToDownload = new FileInputStream(fileObj);
        bis = new BufferedInputStream(fileToDownload);
        response.setContentType("application/text/html");
          response.setHeader("ContentDisposition","attachment;filename="+dump+".html");
        byte[] barray = new byte[BUFFER_SIZE];
        while ((bis.read(barray, 0, BUFFER_SIZE)) != -1) {
            bos.write(barray, 0, BUFFER_SIZE);
        }
        bos.flush();

I am writing download servlet that reads a html file and writes to servletOutputStream, the problem right at the of the file transferred it is adding some garbage data any suggestions about this,

below is code I am using for this


        int BUFFER_SIZE = 1024 * 8;
        servOut   = response.getOutputStream();
        bos       = new BufferedOutputStream(servOut);
        fileObj = new File(file);
        fileToDownload = new FileInputStream(fileObj);
        bis = new BufferedInputStream(fileToDownload);
        response.setContentType("application/text/html");
          response.setHeader("ContentDisposition","attachment;filename="+dump+".html");
        byte[] barray = new byte[BUFFER_SIZE];
        while ((bis.read(barray, 0, BUFFER_SIZE)) != -1) {
            bos.write(barray, 0, BUFFER_SIZE);
        }
        bos.flush();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2024-11-23 23:05:40

bis.read 返回读取的字节数。您需要在 write 调用中考虑到这一点。

像这样的东西:

int rd;
while ((rd=bis.read(...)) != -1) {
     bos.write(..., rd);
}

bis.read returns the number of bytes read. You need to take that into account in your write call.

Something like:

int rd;
while ((rd=bis.read(...)) != -1) {
     bos.write(..., rd);
}
花开半夏魅人心 2024-11-23 23:05:40

问题出在代码的以下部分:

        while ((bis.read(barray, 0, BUFFER_SIZE)) != -1) {
            bos.write(barray, 0, BUFFER_SIZE);
        }

您总是写出 BUFFER_SIZE 字节的倍数,即使您的输入大小不是 BUFFER_SIZE< 的倍数/代码>。这会导致在最后一个块的末尾写入垃圾。

你可以像这样修复它:

        int read;
        while ((read = bis.read(barray, 0, BUFFER_SIZE)) != -1) {
            bos.write(barray, 0, read);
        }

The problem is with the following part of your code:

        while ((bis.read(barray, 0, BUFFER_SIZE)) != -1) {
            bos.write(barray, 0, BUFFER_SIZE);
        }

You are always writing out a multiple of BUFFER_SIZE bytes, even if the size of your input isn't a multiple of BUFFER_SIZE. This results in garbage being written at the end of the last block.

You can fix it like so:

        int read;
        while ((read = bis.read(barray, 0, BUFFER_SIZE)) != -1) {
            bos.write(barray, 0, read);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文