【问题标题】:Spring Boot - redirecting to url and provide file DownloadSpring Boot - 重定向到 url 并提供文件下载
【发布时间】:2017-05-22 11:16:27
【问题描述】:

Spring 中有什么方法可以提供文件下载和视图? 我想给用户一个自动下载并通知用户一个新的 html 页面。

 @RequestMapping(value = "/abgeschlossen/{bildcodes}")
 @ResponseBody
  public byte[]  download(@PathVariable List<String> bildcodes,
  HttpServletRequest request, HttpServletResponse response) throws 
IOException, ServletException {

    response.setContentType("application/zip");
    response.setStatus(HttpServletResponse.SC_OK);
    response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");

    //creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
    ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);


    //packing files
    for (int i = 0;i<bildcodes.size();i++){
        Bild a = bildDao.findBildByCode(bildcodes.get(i));
        if (a!=null) {
            //new zip entry and copying inputstream with file to zipOutputStream, after all closing streams
            zipOutputStream.putNextEntry(new ZipEntry("Bild" + i + ".jpg"));
            ByteArrayInputStream fileInputStream = new ByteArrayInputStream(a.getDatei());

            IOUtils.copy(fileInputStream, zipOutputStream);

            fileInputStream.close();
            zipOutputStream.closeEntry();
        }
    }

    if (zipOutputStream != null) {
        zipOutputStream.finish();
        zipOutputStream.flush();
        IOUtils.closeQuietly(zipOutputStream);
    }

    IOUtils.closeQuietly(bufferedOutputStream);
    IOUtils.closeQuietly(byteArrayOutputStream);
    //RequestDispatcher rd=request.getRequestDispatcher("/abgeschlossen/fertig");
    //rd.forward(request, response);//method may be include or forward
    return byteArrayOutputStream.toByteArray();
}

我同时需要返回我的视图

return "finished.html";

我在 HTMLresponse 中找到了一些解决方案,但我遇到了许多重定向的问题。

希望有人可以帮助我。

【问题讨论】:

    标签: spring spring-mvc model-view-controller


    【解决方案1】:

    您可以将 AJAX 调用添加到 finished.html

    调用AJAX下载脚本,启动成功后重定向到finished.html

    Handle file download from ajax post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 2021-02-03
      • 2010-10-26
      • 2020-06-09
      • 2016-09-11
      相关资源
      最近更新 更多