package hls.plugin.weixin.qiye;

import hls.plugin.weixin.common.GetPropertiesValue;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ZuLinServlet extends HttpServlet
{
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    InputStream in = null;
    OutputStream out = null;
    GetPropertiesValue gp = new GetPropertiesValue();

    String pck_id = request.getParameter("pck_id");
    String file_name = request.getParameter("fileName") + "." + request.getParameter("filetype");
    String file_size = request.getParameter("fileSize");
    String userId = request.getParameter("userId");
    int userId2 = Integer.parseInt(userId);
    String tableName = request.getParameter("tableName");
    String table_pk_value = request.getParameter("table_pk_value");
    int file_size_int = Integer.parseInt(file_size);
    String file_type_code = request.getParameter("filetype");
    String mime_type = (String)gp.GetValue().get("file_type_code") + "/" + request.getParameter("filetype");

    String filepath = 
      (String)gp.GetValue()
      .get("zuLinsavePath") + 
      "/" + file_name;
    System.out.println("filepath>>>" + filepath);
    try {
      in = request.getInputStream();
      out = new FileOutputStream(filepath);
      int bytes = -1;
      byte[] bufferOut = new byte[1024];
      while ((bytes = in.read(bufferOut)) != -1)
        out.write(bufferOut, 0, bytes);
    }
    catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.flush();
        out.close();
      }

    }

    FndAtmAttachment fnd = new FndAtmAttachment(table_pk_value, file_type_code, mime_type, file_name, file_size_int, filepath, userId2, tableName, pck_id);
    boolean ss = new WeCatDaoImpl().save(fnd);
    System.out.println(ss);
  }
}