/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.basic;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import jp.co.sint.config.SIConfig;

import org.apache.log4j.Category;

/**
 * @version $Id: SIPDFUtil.java,v 1.0 2003/12/15 Exp $
 * @author  Jinwang Chen
* <br>Description:
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 *  ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * Jinwang Chen   2003/12/15 14:13:15  Original
 */
public class SIPDFUtil {
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
  private static final String CONTENT_TYPE = "text/html;charset=MS932"; // 7.4.0 ST2060 修正

  public SIPDFUtil(){
  }

  public void execute(HttpServletResponse lResponse,String lPDFFileName,String lDownFileName){
    lResponse.setContentType(CONTENT_TYPE);
    lResponse.setContentType("application/PDF");
    lResponse.setHeader("Content-Disposition","attachment; filename=\""+lDownFileName+"\"");
    ServletOutputStream lOutStream =null;
    BufferedInputStream lInputStream=null;

    try {
      lOutStream = lResponse.getOutputStream();
      lInputStream = new BufferedInputStream(new FileInputStream(lPDFFileName));

      int data;
      while((data=lInputStream.read())!=-1){
        lOutStream.write(data);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally{
      if (lOutStream!=null){//
        try {
          lOutStream.flush();
          lOutStream.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      if (lInputStream!=null){
        try {
          lInputStream.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
  }
}
