package com.dfsj.geoservertool.maptools;
/**
* Created by zfr on 2017/7/26.
*/
import com.dfsj.geoservertool.entity.Lnglat;
import com.dfsj.geoservertool.entity.Tile;
import org.apache.log4j.Logger;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class DownloadThread extends Thread {
private static int BUFFER_SIZE = 1024 * 8;// 缓冲区大小
private static int MAX_TRY_DOWNLOAD_TIME = 128;
private static int CURRENT_PROXY = 0;
private String threadName = "task download googleTiles";
private String dir;
// private int level;
private String tmpDir;
private Proxy proxy;
private int[][] requests;
private String type = ".jpeg";
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Logger log = Logger.getLogger(DownloadThread.class);
public DownloadThread(String threadName, String dir, int[][] requests) {
this.threadName = threadName;
this.dir = dir;
this.requests = requests;
}
@Override
public void run() {
Date now = new Date();
// System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t开始运行");
long t1 = System.currentTimeMillis();
long totalLength = download();
long t2 = System.currentTimeMillis();
double speed = (double) totalLength / (t2 - t1);
now = new Date();
if (speed < 0.5) {
CURRENT_PROXY++;
}
// System.out.println(dateFormat.format(now) + "\t" + threadName + ":\t完成运行\t" + speed + "kB/s");
}
public long download() {
long totalLength = 0;
if (requests == null) {
return 0;
}
for (int i = 0; i < requests.length; i++) {
int xx = requests[i][0]; //列
int yy = requests[i][1]; //行
int zz = requests[i][2]; //级别
//转换关系:TMS 与Google 坐标系的X 、Z是相等的,Y 的转换关系为:Y(TMS) + Y下载(Google) = Math.pow(2,zoom) - 1
int yyg = (int) (Math.pow(2, zz) - 1 - requests[i][1]);
this.tmpDir = dir + "/tmp/" + zz + "/";
File tmpDirFile = new File(tmpDir);
if (tmpDirFile.exists() == false) {
tmpDirFile.mkdirs();
}
String dirStr = dir + zz + "/" + xx + "/";
File fileDir = new File(dirStr);
if (fileDir.exists() == false) {
fileDir.mkdirs();
}
String fileStr = dirStr + yyg + type;
File file = new File(fileStr);
Random random = new Random(System.currentTimeMillis());
String url = "http://mt"+String.valueOf(random.nextInt(4))+".google.cn/vt/lyrs=s@729&gl=CN&x=" + xx + "&y=" + yy + "&z=" + zz
+ "&format=jpeg";
if (file.exists() == false) {
String tmpFileStr = tmpDir + yyg + type;
boolean r = saveToFile(url, fileStr);
if (r == true) {
// totalLength += cut(tmpFileStr, fileStr);
System.out.println( threadName + ":\t" + zz + "\\" + xx + "_" + yy + type + "\t"+proxy+"\t完成!");
// System.out.println( threadName + ":\t" + zz + "\\" + xx + "_" + yy + type + "\t"+proxy+"\t完成!");
} else {
log.info(threadName + ":\t" + zz + "\\" + xx + "_" + yy + type + "\t"+proxy+"\t失败!");
// System.out.println( threadName + ":\t" + zz + "\\" + xx + "_" + yy + type + "\t"+proxy+"\t失败!");
}
}
else {
System.out.println(+ zz + "\\" + xx + "_" + yy + type + "已经下载!");
}
}
return totalLength;
}
public static long cut(String srcFileStr, String descFileStr) {
try {
// int bytesum = 0;
int byteread = 0;
File srcFile = new File(srcFileStr);
File descFile = new File(descFileStr);
if (srcFile.exists()) { // 文件存在时
InputStream is = new FileInputStream(srcFileStr); // 读入原文件
FileOutputStream os = new FileOutputStream(descFileStr);
byte[] buffer = new byte[1024 * 32];
// int length;
while ((byteread = is.read(buffer)) != -1) {
// bytesum += byteread; //字节数 文件大小
// System.out.println(bytesum);
os.write(buffer, 0, byteread);
}
is.close();
os.close();
}
srcFile.delete();
return descFile.length();
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
return 0;
}
public boolean saveToFile(String destUrl, String fileName) {
int currentTime = 0;
while (currentTime < MAX_TRY_DOWNLOAD_TIME) {
try {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpConnection = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
// 建立链接
url = new URL(destUrl);
// url.openConnection(arg0)
currentTime++;
proxy = ProxyConfig.getProxy(CURRENT_PROXY);
//if (proxy != null) {
// System.out.println(threadName + ":\t切换代理\t" + proxy.address().toString());
//} else {
// System.out.println(threadName + ":\t使用本机IP");
//}
if (proxy == null) {
httpConnection = (HttpURLConnection) url.openConnection();
} else {
httpConnection = (HttpURLConnection) url.openConnection(proxy);
}
httpConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
httpConnection.setConnectTimeout(60000);
httpConnection.setReadTimeout(60000);
// 连接指定的资源
httpConnection.connect();
// 获取网络输入流
bis = new BufferedInputStream(httpConnection.getInputStream());
// 建立文件
fos = new FileOutputStream(fileName);
// System.out.println("正在获取链接[" + destUrl + "]的内容;将其保存为文件[" +
// fileName + "]");
// 保存文件
while ((size = bis.read(buf)) != -1){
// System.out.println(size);
fos.write(buf, 0, size);
}
fos.close();
bis.close();
httpConnection.disconnect();
// currentTime = MAX_TRY_DOWNLOAD_TIME;
break;
} catch (Exception e) {
System.out.println(e);
//e.printStackTrace();
CURRENT_PROXY++;
}
}
if (currentTime < MAX_TRY_DOWNLOAD_TIME) {
return true;
} else {
return false;
}
}
/**
*
* 功能描述:<br>
* 将lnglat坐标系转换为tile坐标系
*
* @param zoom 缩放级别
* @param lnglat
* @return Tile
*
* 修改记录:
*/
public static Tile l