http下载 url

来源:互联网 发布:小鸡手柄连接mac 编辑:程序博客网 时间:2024/05/02 01:52

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

 

原创粉丝点击