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);


                        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