下载文件时使用 PrintWriter

发布于 2024-11-01 05:37:54 字数 1092 浏览 1 评论 0原文

我有一个要求,我需要处理一组文件并从中创建一个压缩的 zip 文件,然后将其用于下载。我正在使用 Servlet 来下载该文件,但下载需要相当长的时间。所以我希望用户知道 servlet 正在通过打印编写器输出消息处理请求,而不是向他显示空白屏幕。但是每次我使用打印编写器向屏幕写入内容时,该消息都需要很长时间才能显示屏幕上显示,并且文件未下载。 我怎样才能做到这一点?有什么想法吗? 谢谢。

这是我的代码

OutputStream oStream = null;
    DataInputStream dInput = null;
    File file = new File(("PATH"));
    int length = 0;
    try{
        DownloadServerLogs.processLogs();
        oStream = res.getOutputStream();
        res.setContentType("application/zip");
        res.setContentLength((int)file.length());
        res.setHeader("Content-Disposition",
                         "attachment;filename=\"" + file.getName() + "\"" );
        byte[] bbuf = new byte[BYTES_DOWNLOAD];
        dInput = new DataInputStream(new FileInputStream(file));

        while ((dInput != null) && ((length = dInput.read(bbuf)) != -1))
        {
            oStream.write(bbuf,0,length);
        }
        dInput.close();
        oStream.flush();
        oStream.close();
    }catch(Exception e){
        Utility.getLogger().error(e.getMessage(), e);
    }

I have a requirement, where i need to process a set of files and create a compressed zip file out of it and then use it for download. I am using a Servlet for downloading that file, but the download takes quite sometime. So i want the user to know that the servlet is processing the request through a print writer output messsage instead of showing him a blank screen.But everytime i use a printwriter to write something to the screen, the message takes a lot of time to show on the screen and the file doesnt download.
How can i achieve this? Any ides?
Thanks.

Here's my code

OutputStream oStream = null;
    DataInputStream dInput = null;
    File file = new File(("PATH"));
    int length = 0;
    try{
        DownloadServerLogs.processLogs();
        oStream = res.getOutputStream();
        res.setContentType("application/zip");
        res.setContentLength((int)file.length());
        res.setHeader("Content-Disposition",
                         "attachment;filename=\"" + file.getName() + "\"" );
        byte[] bbuf = new byte[BYTES_DOWNLOAD];
        dInput = new DataInputStream(new FileInputStream(file));

        while ((dInput != null) && ((length = dInput.read(bbuf)) != -1))
        {
            oStream.write(bbuf,0,length);
        }
        dInput.close();
        oStream.flush();
        oStream.close();
    }catch(Exception e){
        Utility.getLogger().error(e.getMessage(), e);
    }

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

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

发布评论

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

评论(2

凡尘雨 2024-11-08 05:37:54

我想最好使用 Javascript 之类的东西向客户端显示消息(假设您使用 AJAX 来调用 servlet)。

I guess its better to display the message to the client by using something like Javascript (assuming you are using AJAX to invoke the servlet).

顾忌 2024-11-08 05:37:54

嗯,这样做可能不太安全。我想看看下面的答案。这并不是您的确切问题,但可能类似。

从 OutputStream 创建 InputStream 的最有效方法

实际上,您是在同一个线程上读取和写入,我的猜测是,随着 PrintWriter 的添加,您会在某个地方陷入僵局。

Well, it's probably not that safe to do it that way. I'd have a look at the answer below. Not that it's your exact problem, but might be similar.

Most efficient way to create InputStream from OutputStream

Effectively, you are reading and writing on the same thread and my guess is that with the addition of the PrintWriter you are getting deadlocked somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文