Spring MVC - 如何在视频中显示消息,直到下载开始 [英] Spring MVC - How to show a message in view until a download starts

查看:146
本文介绍了Spring MVC - 如何在视频中显示消息,直到下载开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,允许用户下载文件。
问题是,根据发送给控制器的参数,控制器将压缩不同的文件夹并将其发送到客户端,并且拉链可能需要2分钟的时间,而用户被提示用空的浏览器选项卡,一个加载头。



有没有办法我可以向用户显示一条消息,直到文件被压缩,下载真的开始? 请稍候,您的文件正在准备下载!



谢谢!

解决方案

这不是弹簧mvc的特定 - 您可以使用任何类型的基于Web的操作(如servlet或webwork / structs操作)来执行此操作。



用户发送执行下载的请求不应该是创建.zip,而是排队创建zip的任务,然后另一个线程(线程?)可以拉出并创建这些.zip文件。



例如,

 类CreateZipController扩展AbstractController {
// tokenService是soemthing,您可以保存未来,并返回一个令牌 - 可以是任何可靠的...像地图一样。
public ModelAndView handleRequestInternal(final HttpServletRequest request,HttpServletResponse response)throws Exception {
Future< ZipFile> zipTask = executorService.submit(new Callable< ZipFile>(){
public ZipFile call(){
return createZipFile(request);
}
});
String token = tokenService.saveTask(zipTask);
if(zipTask.isDone(){
ModelAndView mav = new ModelAndView(downloadView);
mav.addObject(url,getDownloadUrlFrom(zipTask.get()。getName ));
return mav;
}
ModelAndView mav = new ModelAndView(waitView);
mav.addObject(message,请等待zip被创建);
mav.addObject(token,token);
return mav;
}
}


class GetZipController extends abstractController {

public ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response)throws异常{
String token = request.getParameter(token);
Future< ZipFile> zipTask = tokenService .getTaskFrom(token);
if(zipTask.isDone(){
ModelAndView mav = new ModelAndView(downloadView);
mav.addObject(url,getDownloadUrlFrom(zipTask.get ().getName()));
返回mav
} else {
ModelAndView mav = new ModelAndView(waitView);
mav.addObject(message,请稍候正在建立zip);
return mav;
}
}
}

由于我不太熟悉spring mvc,以上可能会稍微关闭(在图书馆名称/惯例方面),但是基本的想法是 - 将zip文件创建排列成一个任务(使用类似 ExecutorService ),然后快速渲染视图。视图本身然后刷新(使用元刷新标记),或者AJAX轮询另一个控制器以查看任务是否完成。如果是,则重定向到下载网址。


I have a Controller which allows users to download a file. The problem is that depending on the params sent to the controller, the controller will zip different folders and send them to the clients, and zipping can take sometimes 2 minutes, time while the user is prompted with an empty browser tab and a loading header.

Is there a way I could show a message to the user until the file is zipped and download really starts? Something like "Please wait, your file is being prepared for download!"

Thank you!

解决方案

This is not specific to spring mvc - you can do this with any sort of web based action (like servlets or webwork/structs actions).

The request the user sends to perform the download should not be creating the .zip, but queue a task that creates the zip, which then another thread (threads?) can pull off and create those .zip files.

E.g,

class CreateZipController extends AbstractController {
//tokenService is soemthing that you can save a future with, and return a token for - can be anything reallly...like a map.
    public ModelAndView handleRequestInternal(final HttpServletRequest request, HttpServletResponse response) throws Exception {
        Future<ZipFile> zipTask = executorService.submit(new Callable<ZipFile>(){
            public ZipFile call() {
                 return createZipFile(request);
            }
        });
        String token = tokenService.saveTask(zipTask);
        if (zipTask.isDone() {
           ModelAndView mav = new ModelAndView("downloadView");
           mav.addObject("url", getDownloadUrlFrom(zipTask.get().getName()));
           return mav;
        }
        ModelAndView mav = new ModelAndView("waitView");
        mav.addObject("message", "Please wait while zip is being created");
        mav.addObject("token", token);
        return mav;        
    }
}


class GetZipController extends AbstractController {

    public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String token = request.getParameter("token");
        Future<ZipFile> zipTask = tokenService.getTaskFrom(token);
        if (zipTask.isDone() {
           ModelAndView mav = new ModelAndView("downloadView");
           mav.addObject("url", getDownloadUrlFrom(zipTask.get().getName()));
           return mav;        
        } else {
           ModelAndView mav = new ModelAndView("waitView");
           mav.addObject("message", "please wait while the zip is being built");
           return mav;
        }
    }
}

Since i m not too familiar with spring mvc, the above might be slightly off (in terms of library names/conventions) but the basic idea is there - queue up the zip file creation as a task (using something like ExecutorService), and then quickly render the view. The view itself then either refreshes (use meta refresh tag), or AJAX poll another controller to see if the task is done. If it is, then redirect to the download url.

这篇关于Spring MVC - 如何在视频中显示消息,直到下载开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆