package com.zhongdao.jlr.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HttpClientDownload {

	private ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
	private MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
	private boolean stop = false;

	protected final Log log = LogFactory.getLog(HttpClientDownload.class);

	private String fileUrl = "http://umsa.careland.com.cn/tmc/get_zone_traffic_snapshoot.ums?cdid=30000&cpid=12&kcard=108001100000002007697598&devidcode=22844C70CCTJ1&actcode=7142549098995416&verid=4C707502252C&zlibcomp=1";

	private int buffer = 1024 * 10;

	private String fileName = "TSPService.txt";

	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileUrl() {
		return fileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}

	public int getBuffer() {
		return buffer;
	}

	public void setBuffer(int buffer) {
		this.buffer = buffer;
	}

	public byte[] getData() {
		return dataStream.toByteArray();
	}

	public ByteArrayOutputStream getDataStream() {
		return dataStream;
	}

	public void setDataStream(ByteArrayOutputStream dataStream) {
		this.dataStream = dataStream;
	}

	public boolean download() {
		HttpClient client = null;
		GetMethod httpGet = null;
		try {
			manager.getParams().setConnectionTimeout(2000);
			manager.getParams().setSoTimeout(2000);
			client = new HttpClient(manager);
			httpGet = new GetMethod(fileUrl);
			httpGet.setRequestHeader("Connection", "close");
			client.executeMethod(httpGet);
			String path = httpGet.getPath();

			int offset = path.lastIndexOf("/");
			if (offset != -1) {
				fileName = path.substring(offset + 1);
			} else {
				fileName = path;
			}
			if (fileName.indexOf(".") == -1) {
				fileName = fileName + ".htm";
			}
			if (fileName.equals(".htm")) {
				fileName = "123.htm";
			}
			InputStream in = httpGet.getResponseBodyAsStream();

			byte[] b = new byte[buffer];
			int len = 0;
			while ((len = in.read(b)) != -1 && !isStop()) {
				dataStream.write(b, 0, len);
			}
			in.close();
			dataStream.close();
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {

				httpGet.releaseConnection();
				client.getHttpConnectionManager().closeIdleConnections(0);
			} catch (Exception e) {
				// log.error(e);
				return false;
			}
		}
		return true;
	}
	
	
   /*
	public static void main(String args[]) {
		HttpClientDownload d = new HttpClientDownload();
		d.download();
		ZipSeparate zp = new ZipSeparate();
		if (d.download()) {
			byte[] httpb = d.getData();
			zp.setFirstLength(768);
			zp.setData(httpb);
			zp.separateData();
			List<byte[]> datas = zp.getDatas();
			DownFileSession ds = new DownFileSession();
			ds.setVehicleId("1");
			ds.setTime(new Date());
			ds.setSessionId(SessionIdUtil.getSessionId());
			ds.setDatas(datas);
			System.out.println(zp.getZipData().length);
			System.out.println(ds.getSize());
		}
	}
	*/
}
