1 5 package com.oreilly.servlet; 6 7 import java.io.*; 8 import javax.servlet.*; 9 import javax.servlet.http.*; 10 11 21 public class MultipartFilter implements Filter { 22 23 private FilterConfig config = null; 24 private String dir = null; 25 26 public void init(FilterConfig config) throws ServletException { 27 this.config = config; 28 29 dir = config.getInitParameter("uploadDir"); 32 if (dir == null) { 33 File tempdir = (File) config.getServletContext() 34 .getAttribute("javax.servlet.context.tempdir"); 35 if (tempdir != null) { 36 dir = tempdir.toString(); 37 } 38 else { 39 throw new ServletException( 40 "MultipartFilter: No upload directory found: set an uploadDir " + 41 "init parameter or ensure the javax.servlet.context.tempdir " + 42 "directory is valid"); 43 } 44 } 45 } 46 47 public void destroy() { 48 config = null; 49 } 50 51 public void doFilter(ServletRequest request, ServletResponse response, 52 FilterChain chain) throws IOException, ServletException { 53 HttpServletRequest req = (HttpServletRequest) request; 54 String type = req.getHeader("Content-Type"); 55 56 if (type == null || !type.startsWith("multipart/form-data")) { 58 chain.doFilter(request, response); 59 } 60 else { 61 MultipartWrapper multi = new MultipartWrapper(req, dir); 62 chain.doFilter(multi, response); 63 } 64 } 65 } 66 | Popular Tags |