Send filter : Filter « Servlets « Java






Send filter

 
package com.java2s;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class SendFilter implements Filter {

  private final static String PDF_DIR = "d:\\book\\java2s";

  private final static String PDF_CONTENT_TYPE = "application/pdf";

  private FilterConfig config;

  /** Creates new SessionFilter */
  public SendFilter() {
  }

  public void init(FilterConfig filterConfig) throws ServletException {

    System.out.println("Instance created of " + getClass().getName());
    this.config = filterConfig;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws java.io.IOException, ServletException {

    //get the file name from the 'file' parameter
    String fileName = request.getParameter("file");
    if (fileName == null || fileName.equals(""))
      throw new ServletException(
          "Invalid or non-existent file parameter in SendPdf component.");

    if (fileName.indexOf(".pdf") == -1)
      fileName = fileName + ".pdf";

    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    HttpServletResponse httpResp = null;
    try {

      httpResp = (HttpServletResponse) response;
      stream = httpResp.getOutputStream();
      File pdf = new File(PDF_DIR + "/" + fileName);

      //set response headers
      httpResp.setContentType(PDF_CONTENT_TYPE);
      httpResp.addHeader("Content-Disposition", "attachment; filename="
          + fileName);
      httpResp.setContentLength((int) pdf.length());

      FileInputStream input = new FileInputStream(pdf);
      buf = new BufferedInputStream(input);
      int readBytes = 0;
      //read from the file; write to the ServletOutputStream
      while ((readBytes = buf.read()) != -1)
        stream.write(readBytes);

    } catch (Exception ioe) {

      //  throw new ServletException(ioe.getMessage());
      System.out.println(ioe.getMessage());

    } finally {

      if (buf != null)
        buf.close();
      if (stream != null) {
        stream.flush();
        //stream.close();
      }

    }//end finally
    chain.doFilter(request, httpResp);
  }

  public void destroy() {
    /*
     * called before the Filter instance is removed from service by the web
     * container
     */
  }
}

           
         
  








Related examples in the same category

1.Filtering page to UTF-8
2.Response Filter
3.Servlets Post Filter Demo
4.Servlets Logging Filter Demo
5.Another Filter Demo
6.Servlets CSV Filter Demo
7.Servlets SortFilter Demo
8.Filter Using Parameter
9.Jsp Using Chained Filter
10.Logging Filter
11.Restricting Filter
12.Filter that performs filtering based on comparing the appropriate request
13.JNDI Filter
14.Email JNDI Filter
15.Log Filter
16.IP Filter
17.Block Filter
18.Checker Filter
19.Servlet : session filter
20.Parameter Filter
21.HTML filter utility
22.Compression Filter
23.Request Filter
24.Filter message string for characters that are sensitive in HTML
25.Filter that wraps an HttpServletRequest to override "isUserInRole".
26.Filter the specified message string for characters that are sensitive in HTML
27.Cache Filter