主要思路:ueditor编辑器上传图片以request请求发送到后台,后台接收通过流的形式进行处理,我们只要在后台拦截到图片文件并进行加水印处理就能够实现该功能。
一、 下载ueditor1_4_3编辑器jsp版,使其能够正常工作;
二、 修改源码
主要修改StorageManager.java文件
1) 添加将上传文件和水印文件合成带水印图片的代码
/** * 将上传文件和水印文件合成带水印图片 */ public static void setWaterMark(File targetFile, String rootPath, String path) throws IOException { //源文件 Image src = ImageIO.read(targetFile); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(src, 0, 0, width, height, null); // 水印文件 String FILENAME = rootPath + “ueditor/image/waterMark.png”; //FILENAME为url地址时,如:http://www.baidu.com/abc.png // URL url = new URL(FILENAME); // InputStream pressIs = url.openStream(); //FILENAME为本地路径时,如:D:/abc.png InputStream pressIs = new FileInputStream(FILENAME); Image src_biao = ImageIO.read(pressIs); int width_biao = src_biao.getWidth(null); int height_biao = src_biao.getHeight(null); g.drawImage(src_biao, width - width_biao, height - height_biao, width_biao, height_biao, null); g.dispose(); FileOutputStream out = new FileOutputStream(path); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } |
2) 修改saveTmpFile方法
private static State saveTmpFile(File tmpFile, String rootPath, String path, Long maxSize) { State state = null; File targetFile = new File(path); if (targetFile.canWrite()) { return new BaseState(false, AppInfo.PERMISSION_DENIED); } try { FileUtils.moveFile(tmpFile, targetFile); } catch (IOException e) { return new BaseState(false, AppInfo.IO_ERROR); } //判断是否为图片文件 if (maxSize == 2048000) { try { //加水印 setWaterMark(targetFile, rootPath, path); } catch (IOException e) { e.printStackTrace(); } } state = new BaseState(true); state.putInfo("size", targetFile.length()); state.putInfo("title", targetFile.getName()); return state; } |
三、 重启上传图片后直接带水印。