package com.jnwat.common.connection;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jnwat.common.config.Global;
import com.jnwat.common.security.DESHelper;
import com.jnwat.common.sqllite.SQLiteUtil;
import com.jnwat.common.utils.StringUtils;
import com.jnwat.robot.IceServiceMgr;
import com.jnwat.robot.core.RICache;
import com.jnwat.robot.core.XmlUtlis;
import com.jnwat.web.api.util.X509TrustUtiil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
/**
* 将图片下载到本地 ,获取本地图片宽高 , base64数据 , 删除图片
*/
public class aa {
public static void main(String[] args) {
// String localPath = "E:\\Lnint_CentralWeb\\FileServer\\MapCfg\\3\\2d.png";
// System.out.println(imageToBase64Str(localPath));
// 获取http图片信息
String httpPath = "http://192.168.11.115:8087/m/3/2d.png";
System.out.println(getImgData(httpPath));
// 获取https图片信息
String httpsPath = "https://192.168.11.115:18088/m/3/2d.png";
System.out.println(getImgData(httpsPath));
}
// 将图片下载到本地 ,获取本地图片宽高 , base64数据 , 删除图片
public static Map<String,String> getImgData(String imgurl){
Map<String,String> imgMap = new HashMap<String,String>();
if(StringUtils.isBlank(imgurl)){
return imgMap;
}
//设置写入路径以及图片名称,判断是否下载本地成功,为空则下载失败
String localPath = System.getProperty("user.dir") + File.separator
+ String.valueOf(System.currentTimeMillis()) + ".png";
if(imgurl.toLowerCase(Locale.ENGLISH).startsWith("https")){
localPath = dowmImgHttps(localPath, imgurl);
}else{
localPath = dowmImgHttp(localPath, imgurl);
}
// 文件下载成功,开始获取宽高和图片数据
if(StringUtils.isNotBlank(localPath)){
BufferedImage bi = null;
try {
URL u = new URL("file:///" + localPath);
bi = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
Image img = bi;
imgMap.put("width",""+bi.getWidth(null));
imgMap.put("height",""+bi.getHeight(null));
String imageToBase64Str = imageToBase64Str(localPath);
imgMap.put("imageToBase64Str",imageToBase64Str);
// 删除图片
File file = new File(localPath);
file.delete();
}
return imgMap;
}
// 下载https地址的图片
public static String dowmImgHttps(String localPath, String imgurl){
// 创建SSLContext
SSLContext sslContext = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
InputStream inputStream = null;
try {
sslContext = SSLContext.getInstance("SSL");
TrustManager[] tm = { new X509TrustUtiil() };
// 初始化
sslContext.init(null, tm, new SecureRandom());
// 获取SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
// url对象
URL url = null;
url = new URL(imgurl);
// 打开连接
HttpsURLConnection conn = null;
conn = (HttpsURLConnection) url.openConnection();
/**
* 这一步的原因: 当访问HTTPS的网址。您可能已经安装了服务器证书到您的JRE的keystore
* 但是服务器的名称与证书实际域名不相等。这通常发生在你使用的是非标准网上签发的证书。
* 解决方法:让JRE相信所有的证书和对系统的域名和证书域名。
* 如果少了这一步会报错:java.io.IOException: HTTPS hostname wrong: should be <localhost>
*/
conn.setHostnameVerifier(new X509TrustUtiil().new TrustAnyHostnameVerifier());
// 设置一些参数
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置当前实例使用的SSLSoctetFactory
conn.setSSLSocketFactory(ssf);
conn.connect();
// 得到输入流
inputStream = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
bos = new ByteArrayOutputStream();
while ((len = inputStream.read(b)) > 0) {
bos.write(b, 0, len);
}
byte[] getData = bos.toByteArray();
// 文件保存位置
File file = new File(localPath);
//输出流
fos = new FileOutputStream(file);
fos.write(getData);
} catch (NoSuchAlgorithmException | KeyManagementException | IOException e) {
localPath = "";
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return localPath;
}
// 下载http地址的图片
public static String dowmImgHttp(String localPath, String imgurl){
try {
//实例化url
URL url = new URL(imgurl);
//载入图片到输入流
BufferedInputStream bis = new BufferedInputStream(url.openStream());
//实例化存储字节数组
byte[] bytes = new byte[1024];
OutputStream bos = new FileOutputStream(new File( localPath));
int len;
while ((len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
//关闭输出流
bis.close();
bos.flush();
bos.close();
} catch (Exception e) {
//如果图片未找到
localPath = "";
}
return localPath;
}
// 获取本地图片base64加密数据
public static String imageToBase64Str(String localPath) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(localPath);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
}