URLConnection
来源:互联网 发布:java编写游戏 编辑:程序博客网 时间:2024/05/22 10:47
public static byte[] getImageFileByURL(String url) throws TpDealException{
long startTime = System.currentTimeMillis();//start time
if(StringUtils.isEmpty(url))return null;
byte[] content = null;
BufferedInputStream in=null;
ByteArrayOutputStream out = null;
URL u;
for(int i = 1; i <= RETRY_TIMES; ++i){
try {
u = new URL(url);
URLConnection uc = u.openConnection();
uc.setConnectTimeout(3000);
uc.setReadTimeout(5000);
in = new BufferedInputStream(uc.getInputStream(), 1024);
byte[] bytes = new byte[1024];
int len;
out = new ByteArrayOutputStream();
while ((len = in.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
in.close();
content = out.toByteArray();
break;
} catch (MalformedURLException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
if(i == RETRY_TIMES){
throw new TpDealException(TpDealExceptionType.WENAN, DealExceptionMsg.IMG_URL_EXCEPTION.getIndex(),
e.toString());
}
} catch (IOException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
if(i == RETRY_TIMES){
throw new TpDealException(TpDealExceptionType.WENAN, DealExceptionMsg.IMG_GET_EXCEPTION.getIndex(),
e.toString());
}
}finally{
if (in != null)
try {
in.close();
} catch (IOException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
throw new TpDealException(TpDealExceptionType.WENAN, DealExceptionMsg.IMG_STREAM_IN_EXCEPTION.getIndex(),
e.toString());
}
if (out != null)
try {
out.close();
} catch (IOException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
}
}
logger.info("ImageUtil_getImageFileByURL successfully, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url);
return content;
}
long startTime = System.currentTimeMillis();//start time
if(StringUtils.isEmpty(url))return null;
byte[] content = null;
BufferedInputStream in=null;
ByteArrayOutputStream out = null;
URL u;
for(int i = 1; i <= RETRY_TIMES; ++i){
try {
u = new URL(url);
URLConnection uc = u.openConnection();
uc.setConnectTimeout(3000);
uc.setReadTimeout(5000);
in = new BufferedInputStream(uc.getInputStream(), 1024);
byte[] bytes = new byte[1024];
int len;
out = new ByteArrayOutputStream();
while ((len = in.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
in.close();
content = out.toByteArray();
break;
} catch (MalformedURLException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
if(i == RETRY_TIMES){
throw new TpDealException(TpDealExceptionType.WENAN, DealExceptionMsg.IMG_URL_EXCEPTION.getIndex(),
e.toString());
}
} catch (IOException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
if(i == RETRY_TIMES){
throw new TpDealException(TpDealExceptionType.WENAN, DealExceptionMsg.IMG_GET_EXCEPTION.getIndex(),
e.toString());
}
}finally{
if (in != null)
try {
in.close();
} catch (IOException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
throw new TpDealException(TpDealExceptionType.WENAN, DealExceptionMsg.IMG_STREAM_IN_EXCEPTION.getIndex(),
e.toString());
}
if (out != null)
try {
out.close();
} catch (IOException e) {
logger.error("ImageUtil_getImageFileByURL exception, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url, e);
throw new TpDealException(TpDealExceptionType.WENAN,
DealExceptionMsg.IMG_STREAM_OUT_EXCEPTION.getIndex(), e.toString());
}}
}
logger.info("ImageUtil_getImageFileByURL successfully, use time: " + (System.currentTimeMillis() - startTime) + ", URL = " + url);
return content;
}
0 0
- URLConnection
- URLConnection
- urlConnection
- URLConnection
- URLConnection
- URLConnection
- URLConnection
- URLConnection
- URLConnection
- URLConnection
- URLConnection
- UrlConnection
- URLConnection
- urlConnection
- URLConnection 详细
- HTTP URLConnection
- URLConnection简述!!!!
- URLConnection 详解
- AndroidImageSlider 滚动图片 viewpager
- 会场安排问题
- C++ 初始化列表
- 《电路基础》有源元件、支路、节点、回路
- .NET WinForm读取扫描枪从COM口传过来的数据,显示在TextBox中,并做其他操作。
- URLConnection
- 第十九课关于记忆存取——C#计算器编程教学
- java虚拟机(JVM)
- 网络拓扑结构的绘制(三)
- iOS绘图教程
- JavaWeb学习总结(五十一)——邮件的发送与接收原理
- Tika文本抽取实例
- C++ 设计一个类,只能生成一个对象
- SVM - multi label