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(); + } + } + } + /** * */