public static Bitmap loadBitmap(String url, UrlBitmapDownloadCallback callback) { if (TextUtils.isEmpty(url)) { return null; } SoftReference<Bitmap> sf = mapUrlToBitmap.get(url); Bitmap bmp = null; if (sf != null) { bmp = sf.get(); } if (bmp == null) { String filePath = FilePaths.getUrlFileCachePath(url); if (new File(filePath).exists()) { BitmapFactory.Options op = new BitmapFactory.Options(); SystemUtils.computeSampleSize(op, filePath, 1024, 1024 * 512); try { bmp = BitmapFactory.decodeFile(filePath, op); } catch (OutOfMemoryError e) { e.printStackTrace(); op.inSampleSize *= 2; bmp = BitmapFactory.decodeFile(filePath, op); } } if (bmp == null) { if (callback != null) { mapUrlToCallback.put(url, callback); } requestDownloadBitmap(url, filePath); } else if (bmp != null) { mapUrlToBitmap.put(url, new SoftReference<Bitmap>(bmp)); } } return bmp; }
@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); }