@Override public OutputStream watermark1(String ossURL,String logoOsskey,HttpServletResponse response) { lock.lock(); OutputStream os = null; InputStream imagestream=null; //水印 String logoPath = ossService.getossURL(logoOsskey,bucket); //原图ossURL try { os=response.getoutputStream(); imagestream = getimagestream(ossURL); Image image2 = ImageIO.read(imagestream); //获取原图信息 int width = image2.getWidth(null); int height = image2.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image2,width,null);// //设置多个图片水印 InputStream logo = getimagestream(logoPath); Image imagelogo = ImageIO.read(logo); int logoWidth = imagelogo.getWidth(null); int logoHeight = imagelogo.getHeight(null); //设置透明度,ALPHA为接口中自定义的值透明度 0.3F g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA)); //完成图片旋转30 g.rotate(Math.toradians(0),bufferedImage.getWidth() / 2,bufferedImage.getHeight() / 2); int x = -width / 2; int y = -height / 2; while (x < width * 1.5) { y = -height / 2; while (y < height * 1.5) { g.drawImage(imagelogo,x,y,null); y += logoHeight + 200; } x += logoHeight + 300; } g.dispose(); Thumbnails.Builder thumbnail = Thumbnails.of(bufferedImage); thumbnail.size(x,y); ImageIO.write(thumbnail.asBufferedImage(),"png",os); //JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //en.encode(bufferedImage); } catch (IOException e) { e.printstacktrace(); } finally { if (os != null) { try { os.flush(); os.close(); } catch (IOException e) { e.printstacktrace(); } } if(imagestream!=null){ try { imagestream.close(); }catch (IOException e){ e.printstacktrace(); } } } lock.unlock(); return os; }