packageins.platform.web.utils;importjava.io.ByteArrayOutputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.MalformedURLException;importjava.net.URL;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;/***
* @Description: 下载图片
*@author:
* @date: 2018年1月5日 上午11:31:05
*@versionV1.0
* @Copyright: 2018 www.newtouch.cn . All rights reserved.
**/
public classPictureDownLoadUtil {private static final Logger LOGGER = LoggerFactory.getLogger(PictureDownLoadUtil.class);private static final int PROTECTED_LENGTH = 1024 * 1024;//输入流保护 1M
public staticString downLoad(String strUrl, Boolean isProxy, String host, String port) {if(isProxy) {
setProxy(host, port);
}
URL url= null;try{
url= newURL(strUrl);
}catch(MalformedURLException e2) {
LOGGER.error("下载图片异常,新建URL失败");return "";
}
InputStream is= null;try{
is= url.openStream();returnreadInfoStreamToBytes(is);
}catch(IOException e1) {
LOGGER.error("下载图片异常");return "";
}
}public static voidsetProxy(String host, String port) {
System.setProperty("proxySet", "true");
System.setProperty("proxyHost", host);
System.setProperty("proxyPort", port);
}public static voidmain(String[] args) {
String str= "http://7xrnr1.com1.z0.glb.clouddn.com/090406.png";
String picStr= PictureDownLoadUtil.downLoad(str, true, "", "");
GenerateImage(picStr);
System.out.print(picStr);
}public static boolean GenerateImage(String imgStr) { //对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) //图像数据为空
return false;
BASE64Decoder decoder= newBASE64Decoder();try{//Base64解码
byte[] b =decoder.decodeBuffer(imgStr);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {//调整异常数据
b[i] += 256;
}
}//生成jpeg图片
String imgFilePath = "d://qq.jpg";//新生成的图片
OutputStream out = newFileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();return true;
}catch(Exception e) {return false;
}
}public staticString readInfoStreamToBytes(InputStream is) {if (is == null) {
LOGGER.error("输入流为null");return null;
}//字节数组
byte[] bCache = new byte[2048];int readSize = 0;//每次读取的字节长度
int totalSize = 0;//总字节长度
ByteArrayOutputStream infoStream = newByteArrayOutputStream();try{//一次性读取2048字节
while ((readSize = is.read(bCache)) > 0) {
totalSize+=readSize;if (totalSize >PROTECTED_LENGTH) {
LOGGER.error("输入流超出1M大小限制");return null;
}//将bcache中读取的input数据写入infoStream
infoStream.write(bCache, 0, readSize);
}
}catch(IOException e1) {
LOGGER.error("输入流读取异常");return null;
}finally{try{
is.close();
}catch(IOException e) {
LOGGER.error("输入流关闭异常");
}
}
BASE64Encoder encoder= newBASE64Encoder();returnencoder.encode(infoStream.toByteArray());
}
}