Java--给图片加水印

//给图片加水印
@WebServlet("/servletDemo01")
public class ServletDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取原始图片的真实路径
        String realPath1 = this.getServletContext().getRealPath("/img/1.jpg");
        BufferedImage image1 = ImageIO.read(new File(realPath1));
        //2.获取水印图片真实路径
        String realPath2 = this.getServletContext().getRealPath("/img/2.png");
        BufferedImage image2 = ImageIO.read(new File(realPath2));
        //3.把image2画到image1的左上角
        image1.getGraphics().drawImage(image2,0,0,null);
        //4.修改图片后返回给浏览器
        ImageIO.write(image1,"png",response.getOutputStream());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
posted @ 2020-08-14 15:45  德华。  阅读(151)  评论(0编辑  收藏  举报