package hls.plugin.weixin.qiye;

import hls.plugin.weixin.common.GetPropertiesValue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class WeiImagePostTo
{
  public void send(String mediaId, String pck_id, String fileName, String filetype, String fileSize, String userId, String tableName, String table_pk_value)
  {
    GetPropertiesValue gp = new GetPropertiesValue();
    HttpURLConnection con = null;
    OutputStream out = null;

    InputStream in = null;
    InputStream inputStream = null;
    ByteArrayOutputStream baos = null;
    int length = 0;
    try {
      File file = new File((String)gp.GetValue().get("winXinSavePath") + "/" + 
        fileName + "." + filetype);
      if ((!file.exists()) || (!file.isFile())) {
        throw new IOException("文件不存在");
      }

      String enCodemediaId = mediaId;
      /*URL urlObj = new URL((String)gp.GetValue().get("zuLinserviceUrl") + 
        "?mediaId=" + enCodemediaId + "&pck_id=" + pck_id + 
        "&fileName=" + fileName + "&filetype=" + filetype + 
        "&fileSize=" + fileSize + "&userId=" + userId + "&tableName=" + tableName + "&table_pk_value=" + table_pk_value);
      */
     
      int userId2 = Integer.parseInt(userId);
      int file_size_int = Integer.parseInt(fileSize);
      String mime_type = (String)gp.GetValue().get("file_type_code") + "/" +filetype;
      String filepath = 
    	      (String)gp.GetValue()
    	      .get("weiXinSavePath") + 
    	      "/" + fileName;
      
      FndAtmAttachment fnd = new FndAtmAttachment(table_pk_value, filetype, mime_type, fileName, file_size_int, filepath, userId2, tableName, pck_id);
      boolean ss = new WeCatDaoImpl().save(fnd);
      System.out.println(ss);
    

    /*  con = (HttpURLConnection)urlObj.openConnection();

      con.setDoOutput(true);
      con.setRequestProperty("Content-Length", fileSize);
      con.setRequestProperty("Content-type", "application/image");
      con.setRequestMethod("POST");

      con.connect();

      out = con.getOutputStream();
      in = new FileInputStream(file);
      int bytes = -1;
      byte[] bufferOut = new byte[1024];
      while ((bytes = in.read(bufferOut)) != -1) {
        out.write(bufferOut, 0, bytes);
      }

      inputStream = con.getInputStream();*/
    }
    catch (Exception e) {
      System.out.println("传输异常:" + e.getMessage());

      if (in != null) {
        try {
          in.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      if (out != null) {
        try {
          out.flush();
          out.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      if (con != null) {
        con.disconnect();
      }

      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      if (baos != null)
        try {
          baos.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
    }
    finally
    {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (out != null) {
        try {
          out.flush();
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (con != null) {
        con.disconnect();
      }

      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (baos != null)
        try {
          baos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
  }
}