package com.fyx.yunpan.net;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import android.content.Context;
import android.util.Log;

import com.fyx.yunpan.share.ShareUtils;

public class TouXiangUpLoadWithHttps {

	/* 上传文件至Server的方法 */
	public static void uploadBigFileMethod(File bitmap, ShareUtils su,
			Context context) {
		String actionUrl =  su.getURL()
				+ "/a1/uhead";
		/*
		 * //https用 TrustManager[] trustAllCerts = new TrustManager[] { new
		 * X509TrustManager() { public java.security.cert.X509Certificate[]
		 * getAcceptedIssuers() { return new
		 * java.security.cert.X509Certificate[] {}; }
		 * 
		 * public void checkClientTrusted( java.security.cert.X509Certificate[]
		 * chain, String authType) throws
		 * java.security.cert.CertificateException { }
		 * 
		 * public void checkServerTrusted( java.security.cert.X509Certificate[]
		 * chain, String authType) throws
		 * java.security.cert.CertificateException { } } };
		 * 
		 * SSLContext context = null; try { context =
		 * SSLContext.getInstance("TLS"); context.init(null, trustAllCerts, new
		 * java.security.SecureRandom()); HttpsURLConnection
		 * .setDefaultHostnameVerifier(new myHostnameVerifier());
		 * HttpsURLConnection.setDefaultSSLSocketFactory(context
		 * .getSocketFactory()); } catch (NoSuchAlgorithmException e1) {
		 * e1.printStackTrace(); } catch (KeyManagementException e) {
		 * e.printStackTrace(); }
		 */
		String end = "\r\n";
		String twoHyphens = "--";
		String boundary = "WebKitFormBoundaryLL0Bluv332wLOSam";
		HttpsURLConnection con = null;
		try {
			// System.setProperty("http.keepAlive", "false");
			URL url = new URL(actionUrl);
			// 创建SSLContext对象，并使用我们指定的信任管理器初始化
            TrustManager[] tm = { new MyX509TrustManager() };
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
			 con = (HttpsURLConnection) url.openConnection();
			 con.setSSLSocketFactory(ssf);
			/* 允许Input、Output，不使用Cache */
			System.out.println("uploadBigFileMethod-3");
			
			con.setDoInput(true);
			// con.setChunkedStreamingMode(1024*1024);
			con.setDoOutput(true);
			// con.setRequestProperty("Connection", "close");
			con.setUseCaches(false);
			/* 设置传送的method=POST */
			con.setRequestMethod("POST");
			/* setRequestProperty */
				con.setRequestProperty("Connection", "keep-live");
			// con.setFixedLengthStreamingMode(1024*1024*1024);
			// con.setRequestProperty( "Accept-Encoding", "" );
			con.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			con.setRequestProperty("Cookie", "fuid=" + su.getLoginEntity().getId()
					+ "; token=" + su.getLoginEntity().getToken() + "");
			/* 设置DataOutputStream */
			DataOutputStream ds = new DataOutputStream(con.getOutputStream());

			ds.writeBytes(twoHyphens + boundary + end);
			ds.writeBytes("Content-Disposition: form-data; "
					+ "name=\"Filedata\"; filename=\"1.jgp\"" + end);
			ds.writeBytes("Content-Type: image/jpeg" + end);
			ds.writeBytes(end);

			// String postContent = URLEncoder.encode("aid", "UTF-8")
			// + "=" + URLEncoder.encode(aid+"", "UTF-8") + "&" +
			// URLEncoder.encode("pid", "UTF-8") + "="+
			// URLEncoder.encode(pid+"", "UTF-8")+ "&" +
			// URLEncoder.encode("name", "UTF-8") + "="+
			// URLEncoder.encode(file.getName(), "UTF-8") +"&" +
			// URLEncoder.encode("Filedata", "UTF-8") + "="+
			// URLEncoder.encode(file.getName(), "UTF-8") ;
			// System.out.println(postContent);
			// byte[] bypes = postContent.toString().getBytes();
			// ds.write(bypes);//
			/* 取得文件的FileInputStream */
			// FileInputStream fStream = new FileInputStream(uploadFile);
			/* 设置每次写入1024bytes */
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			int length = -1;
			// 前面 startPosition个头字节读取不上传
			// for (int i = 0; i < startPosition / bufferSize; i++) {
			// fStream.read(buffer);
			// }
			// if (startPosition % bufferSize != 0) {
			// byte[] b = new byte[(int) (startPosition % bufferSize)];
			// fStream.read(b);
			// // }
			// File file = StorageUtils.getCacheDirectory(context);
			// File img = new File(file.getAbsolutePath()+"/1.kkk");
			// FileOutputStream stream = new FileOutputStream(img);
			// boolean flag = bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
			// stream);
			// // bitmap.recycle();
			// stream.close();
			// System.out.println(flag);

			FileInputStream input = new FileInputStream(bitmap);

			/* 从文件读取数据至缓冲区 */
			while ((length = input.read(buffer)) != -1) {
				/* 将资料写入DataOutputStream中 */
				ds.write(buffer, 0, length);
			}

			ds.writeBytes(end);
			ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
			/* close streams */
			ds.flush();
			/* 取得Response内容 */
			InputStream is = con.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			// int ch;
			// System.out.println("goood");
			StringBuffer b = new StringBuffer();
			// while ((ch = is.read()) != -1) {
			// b.append((char) ch);
			// }

			byte[] data = new byte[1024];
			byte[] t = new byte[1024];
			int sum = 0;
			int temp;
			while ((temp = bis.read(t)) != -1) {

				System.arraycopy(t, 0, data, sum, temp);
				sum += temp;
			}

			// entity2return =
			// PrepareFileUploadUtil.parse(UTF2GBK.Unicode2GBK(b.toString()));
			/* 将Response显示于Dialog */

			/* 关闭DataOutputStream */

			ds.close();
			is.close();
			input.close();
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			if (con != null) {
				con.disconnect();
			}
		}
	}

}
