java读取网页链接(转)

java 代码
  1. /**  
  2. * @todo 5) 从链接urlPath读取文件内容,存储到filePath文件中  
  3. * @param filePath String  
  4. * @param urlPath String  
  5. * @return String  
  6. */  
  7. public String createHtmlFile(String filePath, String urlPath)   
  8.   
  9. {   
  10. try  
  11. {   
  12. Util.log("urlPath="+urlPath);   
  13.   
  14. URL url = new URL(urlPath);   
  15. URLConnection urlConnection = url.openConnection();   
  16. urlConnection.setAllowUserInteraction(false);   
  17. // InputStream urlStream = url.openStream();   
  18. InputStream urlStream = urlConnection.getInputStream();//.openStream();   
  19. byte b[] = new byte[1024];   
  20. int numRead = urlStream.read(b);   
  21. String content = new String(b, 0, numRead);   
  22. StringBuffer tempHtml = new StringBuffer(); //   
  23. while ( (numRead != -1) && (content.length() < MAXSIZE))   
  24. {   
  25. numRead = urlStream.read(b);   
  26. if (numRead != -1)   
  27. {   
  28. String newContent = new String(b, 0, numRead);   
  29. content += newContent;   
  30. }   
  31. }   
  32. tempHtml = tempHtml.append(content);   
  33. FileOperation.writeFromBuffer(filePath, tempHtml);   
  34. return content;   
  35. }   
  36.   
  37. catch (IOException e)   
  38. {   
  39. e.printStackTrace();   
  40. Util.log("ERROR: couldn't open URL ");   
  41. return "";   
  42. }   
  43. }   

 

 

为什么不用InputStreamReader去读文件呢,可以用指定编码方式去读取文件,设置读取方式为GBK就可以了



同意,采用如下方法就可以了:

java 代码
  1. /**  
  2. * @todo 5) 从链接urlPath读取文件内容,存储到filePath文件中 中文问题已经解决了  
  3. * @param filePath String  
  4. * @param urlPath String  
  5. * @return String  
  6. */  
  7. public String createHtmlFile(String filePath, String urlPath)   
  8.   
  9. {   
  10. try  
  11. {   
  12. Util.log("urlPath="+urlPath);   
  13.   
  14. URL url = new URL(urlPath);   
  15.   
  16. URLConnection urlConnection = url.openConnection();   
  17. urlConnection.setAllowUserInteraction(false);   
  18. InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());   
  19. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));   
  20. //使用openStream得到一输入流并由此构造一个BufferedReader对象   
  21. String inputLine;   
  22. String content="";   
  23. StringBuffer tempHtml = new StringBuffer(); //   
  24. //从输入流不断的读数据,直到读完为止   
  25. while ((inputLine = in.readLine()) != null)   
  26.   
  27. {   
  28. //content=content+inputLine+"/n";   
  29. //tempHtml.append(inputLine+"/n");   
  30. tempHtml.append(inputLine+"/n");   
  31. }   
  32.   
  33.   
  34.   
  35. //Util.log("*******************************************************");   
  36. //Util.log(content.toString());   
  37. //Util.log("*******************************************************");   
  38. //tempHtml = tempHtml.append(content);   
  39. FileOperation.writeFromBuffer(filePath, tempHtml);   
  40. return content;   
  41. }   
  42.   
  43. catch (IOException e)   
  44. {   
  45. e.printStackTrace();   
  46. Util.log("ERROR: couldn't open URL ");   
  47. return "";   
  48. }   
  49. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值