采用过滤器处理指定盘符下的图片显示

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>JsUtils</display-name>
  <!-- 图片的过滤器注册 -->
  <filter>
	<filter-name>ImgFilter</filter-name>
	<filter-class>com.jing.filter.ImgFilter</filter-class>
  </filter>
  <!-- 图片的过滤器映射 -->
  <filter-mapping>
	<filter-name>ImgFilter</filter-name>
	<url-pattern>/upload_images/poster/*</url-pattern>
  </filter-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>

过滤器ImgFilter.java[主要是用来读取指定盘符位置的图片的文件流]

package com.jing.filter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

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;

import org.apache.log4j.Logger;

import com.jing.comm.AppComm;

/**
 * 图片过滤器
 * @author  jing.yue
 * @version 2012/08/15 1.0.0
 */
public class ImgFilter implements Filter {

	private static final Logger logger = Logger.getLogger(ImgFilter.class);

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;

		String contextPath = request.getContextPath() + "/";
		String uri = request.getRequestURI();
		String file = null;
		if(uri.indexOf(contextPath) != -1) {
			file = AppComm.saveImageRoot + uri.substring(contextPath.length());
		}
		try {
			FileInputStream in = new FileInputStream(new File(file));
			OutputStream o = response.getOutputStream();
			int l = 0;
			byte[] buffer = new byte[4096];
			while ((l = in.read(buffer)) != -1) {
				o.write(buffer, 0, l);
			}
			o.flush();
			in.close();
			o.close();
		} catch (Exception e) {
			logger.error("ERROR: " + e.getMessage());
		}
		//arg2.doFilter(request, arg1);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}

常量类AppComm.java

package com.jing.comm;

public class AppComm {

	//保存图片的根目录
	public static String saveImageRoot = "C:/temp/";
}

你可能感兴趣的:(过滤显示图片)