图片给的绝对路径:G:\workSpace\WaterMarkFilter\WebRoot\WEB-INF\1.jpg
顺便想问下该怎么实现这个过滤器的功能啊,求论坛大神帮忙啊,想好久了啊!!!
Watermark.java代码如下:
package com.Filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WaterMark extends HttpServlet {
byte[] by;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("image/jpeg");
InputStream inputStream=new FileInputStream("G:/workSpace/WaterMarkFilter/WebRoot/WEB-INF/1.jpg");
ServletOutputStream out=response.getOutputStream();
while((inputStream.read(by))!=-1){
}
out.write(by.length);
}
}
//下面实现的是一个Filter类
WaterMarkFilter.java代码如下:
package com.Filter;
import com.Filter.WaterMarkResponseWrapper;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WaterMarkFilter implements Filter {
private String waterMarkFile;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws ServletException,IOException{
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;
WaterMarkResponseWrapper waterMarkRes=new WaterMarkResponseWrapper(response,waterMarkFile);
chain.doFilter(request,waterMarkRes);
waterMarkRes.finishResponse();
}
public void init(FilterConfig filterConfig) throws ServletException {
String file= filterConfig.getInitParameter("Picture");
waterMarkFile=filterConfig.getServletContext().getRealPath(file);
}
}
//这是Response包装类的重写
WaterMarkResponseWrapper.java代码如下:
package com.Filter;
import com.Filter.WaterMarkOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class WaterMarkResponseWrapper extends HttpServletResponseWrapper{
private String WaterMarkFile;
private HttpServletResponse response;
public WaterMarkOutputStream waterMarkOutputStream=new WaterMarkOutputStream();
public WaterMarkResponseWrapper(HttpServletResponse response,String waterMarkFile) throws IOException{
super(response);
this.response=response;
this.WaterMarkFile=waterMarkFile;
}
public ServletOutputStream getOutputStream() throws IOException{
return waterMarkOutputStream;
}
public void flushBuffer() throws IOException{
}
public void finishResponse() throws IOException{
byte [] imageData =waterMarkOutputStream.getByteArrayOutputStream().toByteArray();//原图片数据
byte [] image=ImageUtil.waterMark(imageData,WaterMarkFile);
response.setContentLength(image.length);
response.getOutputStream().write(image);
waterMarkOutputStream.close();
}
}
//这是字节输出流的重写
WaterMarkOutputStream.java代码如下:
package com.Filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
public class WaterMarkOutputStream extends ServletOutputStream{
private ByteArrayOutputStream byteArrayOutputStream;
public WaterMarkOutputStream() throws IOException{
byteArrayOutputStream=new ByteArrayOutputStream();
}
public void write(int b) throws IOException{
byteArrayOutputStream.write(b);
}
public void close() throws IOException{
byteArrayOutputStream.close();
}
public void flush() throws IOException{
byteArrayOutputStream.flush();
}
public void write(byte []b) throws IOException{
byteArrayOutputStream.write(b);
}
public void write(byte []b,int off,int len) throws IOException{
byteArrayOutputStream.write(b,off,len);
}
public ByteArrayOutputStream getByteArrayOutputStream(){
return byteArrayOutputStream;
}
}
//这个类是往图片上添加水印的
ImageUtil.java代码如下
package com.Filter;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
public static byte[] waterMark(byte[] imageData,String waterMarkFile) throws IOException{
int paddingRight=10;
int paddingBottom=10;
Image image=new ImageIcon(imageData).getImage();
int imageWidth=image.getWidth(null);
int imageHeight=image.getHeight(null);
Image WaterMark=ImageIO.read(new File(waterMarkFile));
int WaterMarkWidth=WaterMark.getWidth(null);
int WaterMarkHeight=WaterMark.getHeight(null);
BufferedImage bufferedImage=new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics g=bufferedImage.createGraphics();
g.drawImage(image,0,0,imageWidth,imageHeight,null);
g.drawImage(WaterMark,imageWidth-WaterMarkWidth-paddingRight,imageHeight-WaterMarkHeight-paddingBottom,null);
g.dispose();
ByteArrayOutputStream out=new ByteArrayOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(bufferedImage);
byte [] data=out.toByteArray();
out.close();
return data;
}
}
//Filter的配置
Web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WaterMarkFilter</display-name>
<filter>
<filter-name>WaterMarkFilter</filter-name>
<filter-class>com.Filter.WaterMarkFilter</filter-class>
<init-param>
<param-name>Picture</param-name>
<param-value>/WEB-INF/is.jpg</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WaterMarkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>WaterMark</servlet-name>
<servlet-class>com.Filter.WaterMark</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>WaterMark1</servlet-name>
<servlet-class>com.Filter.WaterMark1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WaterMark</servlet-name>
<url-pattern>/WaterMark</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WaterMark1</servlet-name>
<url-pattern>/WaterMark1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
------解决思路----------------------
G:/workSpace/WaterMarkFilter/WebRoot/WEB-INF/1.jpg 这个文件是否存在,猜测可能是这个文件不存在导致的。
很明显提示的是空指针异常啊。
------解决思路----------------------
首先不要使用绝对路径
web程序会部署在服务器中间件中
另外,建议文件不要放在 WEB-INF 文件夹下
可以新建一个和 WEB-INF 同级的文件夹
------解决思路----------------------
Debug一下,看看那个变量出现问题ile