【发布时间】: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