From 750268fe54888f01440d92f511ff6457725cfb8a Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 27 Oct 2013 21:51:03 +0100 Subject: [PATCH] Add return method for byte[]. --- .classpath | 2 +- .../kiss4web/SimpleServletDispatcher.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/.classpath b/.classpath index c9cb501..f142b09 100644 --- a/.classpath +++ b/.classpath @@ -21,6 +21,6 @@ - + diff --git a/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java b/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java index a237b08..54b416b 100755 --- a/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java +++ b/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java @@ -529,6 +529,69 @@ public class SimpleServletDispatcher extends HttpServlet } } + /** + * + */ + static public void returnFile(final HttpServletResponse response, final String fileName, final byte[] data, final String mimeType, final ContentDispositionType contentDisposition, + final int cacheAge) throws IOException + { + + if (data == null) + { + logger.warn("data is null."); + } + + /* + * response.setContentType("application/" + extension); + * response.setContentLength((int) data.length); + * response.setHeader("Content-Disposition" + * ,"attachment; filename=\"" + filename + "\""); + * response.flushBuffer(); + */ + response.reset(); + response.setContentType(mimeType); + response.setContentLength(data.length); + String contentDispositionToken; + if (contentDisposition == ContentDispositionType.ATTACHMENT) + { + contentDispositionToken = "attachment"; + } + else + { + contentDispositionToken = "inline"; + } + response.setHeader("Content-Disposition", contentDispositionToken + "; filename=\"" + fileName + "\""); + + response.setDateHeader("Expires", new Date().getTime() + cacheAge * 1000); + response.setHeader("Cache-Control", "max-age=" + cacheAge); + + response.flushBuffer(); + + ServletOutputStream out = response.getOutputStream(); + + try + { + out.write(data, 0, data.length); + } + catch (IOException exception) + { + exception.printStackTrace(); + response.sendError(HttpServletResponse.SC_PARTIAL_CONTENT); + } + finally + { + try + { + out.flush(); + out.close(); + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + } + /** * */