关闭

http下载 url

306人阅读 评论(0) 收藏 举报

https://feeds.rss.bigpond.com/xmlfeed?aid=21&feedId=327680&useragent

 

public void downRss() {
  long time1 = System.currentTimeMillis();
  try { 
   
   InputStream inputStream = null;
   URL url = null;
   try {
    url = new URL(baseUrl);
   } catch (MalformedURLException e) {
  /***************************************************************************************/
    String str= "错误的RSSLink.xml下载链接。";
    writelog(str);
 /***************************************************************************************/
   }
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   writelog("url.openConnection成功");
   conn.setConnectTimeout(60 * 1000);
   conn.setDoInput(true); 
   try {
    conn.connect();
   } catch (IOException e1) {
    writelog("conn.connect失败");
    e1.printStackTrace();
   } 
   try {
    inputStream = conn.getInputStream();
   } catch (IOException e1) {
    writelog("conn.getInputStream失败");
    e1.printStackTrace();
   }
   int length = (int) conn.getContentLength();
   
   writelog("LENGTH  :  "+String.valueOf(length));
   
   // 获取文件的大小
   if (length != -1) {
    writelog("进入length判断内部");
    byte[] imgData = new byte[length];
    byte[] temp = new byte[512];
    int readLen = 0;
    int destPos = 0;
    while ((readLen = inputStream.read(temp)) > 0) {
     writelog("readlen  :  "+String.valueOf(readLen));
     System.arraycopy(temp, 0, imgData, destPos, readLen);
     destPos += readLen;
    }
    writelog("ByteArrayInputStream前");
    InputStream isP = new ByteArrayInputStream(imgData);
    writelog("ByteArrayInputStream后");
    fileU.write2SDFromInput("voa/", "RSSlink.xml", isP);
    writelog("write2SDFromInput后");
   }
  } catch (Exception e) {
   dialog("downRss");

  }
  
  /***************************************************************************************/
  File rss = new File(path + "/voa/RSSlink.xml");
  if(!rss.exists())
  {
   String str = "RSSLink.xml下载失败";
   writelog(str);
  }
/***************************************************************************************/
  long time2= System.currentTimeMillis();
  String str = String.valueOf((time2-time1)/1000);
  writelog("RSSlink.xml文件下载时间:  "+str);  
 }

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65508次
    • 积分:359
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:57篇
    • 译文:0篇
    • 评论:4条
    最新评论