private BufferedImage createDisplayImage( CkProperties properties, byte[] imageByteArray, Dimensions currentDimensions) { BufferedImage image; if (currentDimensions.width > properties.getMaxWidth() || currentDimensions.height > properties.getMaxHeight()) { image = ImageUtil.scale(imageByteArray, properties.getMaxWidth(), properties.getMaxHeight()); } else { image = ImageUtil.getBufferedImage(imageByteArray); } return image; }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String error = ""; String contextPath = getServletContext().getContextPath(); String funcNum = req.getParameter("CKEditorFuncNum"); resp.setContentType("text/html"); PrintWriter writer = resp.getWriter(); PropertyProcessor instance = PropertyProcessor.getInstance(contextPath); CkProperties properties = instance.getProperties(); FileItem fileItem = fetchFileFromRequest(req, properties.getUploadFieldName()); String contentType = fileItem.getContentType(); String output = ""; if (contentType.contains("image")) { byte[] bytes = fileItem.get(); Dimensions currentDimensions = ImageUtil.getCurrentDimensions(bytes); BufferedImage displayImage = createDisplayImage(properties, bytes, currentDimensions); BufferedImage thumbnailImage = createThumbnailImage(properties, bytes, currentDimensions); FilePathStrategy strategy = properties.getStrategy(); FilePaths paths = strategy.createPaths(fileItem.getName(), properties); ImageUtil.saveImage(displayImage, paths.getDisplayImageServerPath()); ImageUtil.saveImage(thumbnailImage, paths.getThumbnailServerpath()); output = String.format( "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(%s, '%s', '%s');</script>", funcNum, paths.getDisplayImageRelativeUrl(), error); } else { output = String.format( "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(%s, '%s', '%s');</script>", funcNum, "", properties.getNoImageError()); } writer.write(output); }