这次是因为公司的要求,查看图片信息泄露的人是谁,要求在图片上加姓名以及时间水印
所以我们首先需要在图片上添加倾斜的半透明文字水印,再通过IO流将加完水印的图片返回,这中间可能会发生客户端关闭了图片预览,即不再接收后台数据,后台却依旧在发送数据
接下来直接上代码
public void getCanvasImage(HttpServletResponse response,String imageUrl,String userName)throws IOException {
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String now =formatter.format(date);
now = now.replaceAll("-", "");//将日期的“-”去掉,尽量减少文字长度
String waterMarkContent=userName+now;
URL url=new URL(imageUrl);
OutputStream out =null;
String suffix = null;
try {
// 获取图片后缀
if(imageUrl.indexOf(".") > -1) {
suffix = imageUrl.substring(imageUrl.lastIndexOf(".") + 1);
}