弹出下载文件控制器中的重定向错误处理 [英] Error handling by redirection in spring download file controller

查看:224
本文介绍了弹出下载文件控制器中的重定向错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很好地解释了如何在春天编写下载文件控制器。 这个问题很好地解释了无法使用response.sendRedirect()发送Post请求

This question nicely explains on how to write download file controllers in spring. This question nicely explains that Post request cannot be sent using response.sendRedirect()

我希望将用户重定向到同一页面,并显示导致文件下载错误的错误。以下是工作流程

I would like the user to be redirected to the same page with an error about what caused file download error. Here is the workflow


  1. 用户点击www.abc.com/index [控制器已映射/index.jsp并返回ModelAndView]

  2. 在此页面,我们有一个文件下载,其URL为www.abc.com/download?resource_id=123。 [控制器已映射/下载并返回无效]

  3. 当文件下载出错时,应将用户重定向到www.abc.com/index,并显示错误信息。

  4. 当文件下载没有错误时,用户将停留在同一页面并显示文件下载对话框。

  1. User hits www.abc.com/index [controller has mapping /index.jsp and return ModelAndView]
  2. At this page, we have a file download which has URL www.abc.com/download?resource_id=123. [controller has mapping /download and returns void]
  3. When there is error in file download, user should be redirected to www.abc.com/index with some error display.
  4. When there is no error in file download, user stays at the same page and file download dialog appears.

以下是转发代码:

@RequestMapping(/download)
public void execute(@RequestParam(value = "resource_id" required = true) final String resource, final HttpServletRequest request, final HttpServletResponse response) {
    try {
        //some processing
    } catch {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/index" + "?is_downloaded=false");
        dispatcher.forward(request, response)
    }
}

@RequestMapping(/index)
public void execute(@RequestParam(value = "is_downloaded" required = false) final String isDownloaded, final HttpServletRequest request) {
    //do stuff here
}

第3步是问题所在。


  • 使用转发将URL更改为报告的下载URL时出错。

  • 使用重定向因为带有隐藏参数的response.sendRedirect()是不可能的,根本不会修改URL。

  • 使用redirect作为带有隐藏参数的response.sendRedirect()并引入?is_downloaded = false在URL的末尾

任何人都可以告诉解决方法。

Can anyone tell a workaround for this.

推荐答案

我遇到了类似的问题并用下面的代码解决了这个问题。

I've been having a similar issue and solved it with the below code.

我的下面有一个异常处理程序控制器。如果出现错误,则负责重定向和错误消息传递。

I have the following exception handler in my controller. If there is an error this takes care of the re-direct and error messaging.

@ExceptionHandler(FileNotFoundException.class)
public ModelAndView exceptionHandler(Exception ex) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("rescues/general");
    modelAndView.addObject("message", ex.getMessage());
    return modelAndView;
}

这是RequestMapping

Here is the RequestMapping

@RequestMapping(value = "s3Download.request", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> download(@RequestParam(value = "s3key") String s3Key)
        throws IOException {
    HttpHeaders responseHeaders = new HttpHeaders();
    InputStreamResource inputStreamResource = s3DownloadService.getS3File(s3Key, responseHeaders);
    return new ResponseEntity<>(inputStreamResource, responseHeaders, HttpStatus.OK);
}

如果文件位于服务器本地,则可以使用FileSystemResource代替InputStreamResource。

If the files are locally on your server you can use a FileSystemResource in place of the InputStreamResource.

服务层负责在HttpHeaders对象上设置响应头值

The service layer takes care of setting the response header values on the HttpHeaders object

responseHeaders.setContentType(MediaType.parseMediaType(objectMetaData.getContentType()));
responseHeaders.setContentLength(objectMetaData.getContentLength());
responseHeaders.setContentDispositionFormData("attachment", fileName);

这篇关于弹出下载文件控制器中的重定向错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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