htmlclient 简单使用方法

1.一个根据动态页路径和编码格式获得html格式文本的方法源代码:

Java代码
   1. // 返回html代码  
   2.     public final static String getHtmlCode(String httpUrl, String ecode) {  
   3.         // 构造HttpClient的实例  
   4.         HttpClient httpClient = new HttpClient();  
   5.         // 创建GET方法的实例  
   6.         GetMethod getMethod = new GetMethod(httpUrl);  
   7.         // 使用系统提供的默认的恢复策略  
   8.         getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
   9.                 new DefaultHttpMethodRetryHandler());  
  10.         try {  
  11.             // 执行getMethod  
  12.             int statusCode = httpClient.executeMethod(getMethod);  
  13.             if (statusCode != HttpStatus.SC_OK) {  
  14.                 System.err.println("Method failed: "  
  15.                         + getMethod.getStatusLine());  
  16.             }  
  17.             // 读取内容  
  18.             byte[] responseBody = getMethod.getResponseBody();  
  19.             // 处理内容  
  20.             return new String(responseBody, ecode);  
  21.         } catch (HttpException e) {  
  22.             // 发生致命的异常,可能是协议不对或者返回的内容有问题  
  23.             System.out.println("Please check your provided http address!");  
  24.             e.printStackTrace();  
  25.         } catch (IOException e) {  
  26.             // 发生网络异常  
  27.             e.printStackTrace();  
  28.         } finally {  
  29.             // 释放连接  
  30.             getMethod.releaseConnection();  
  31.         }  
  32.         return null;  
  33.     }  
 2.将html文本写入一个文件中的方法(para1:保存文件名 para2:html文本内容)
   1. public final static void saveHtmlCode(String filePath, String htmlCode) {  
   2.         FileOutputStream fOut = null;  
   3.         OutputStreamWriter out = null;  
   4.         try {  
   5.             fOut = new FileOutputStream(filePath);  
   6.             out = new OutputStreamWriter(fOut, "UTF-8");  
   7.             out.write(htmlCode);  
   8.         } catch (Exception e) {  
   9.             e.printStackTrace();  
  10.         } finally {  
  11.             try {  
  12.                 out.flush();  
  13.             } catch (IOException e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.             try {  
  17.                 fOut.flush();  
  18.             } catch (IOException e) {  
  19.                 e.printStackTrace();  
  20.             }  
  21.             try {  
  22.                 out.close();  
  23.             } catch (IOException e) {  
  24.                 e.printStackTrace();  
  25.             }  
  26.             try {  
  27.                 fOut.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.     }  
  3.下面是一段伪代码.根据当前时间现在c盘建立要保存文件的文件夹,然后保存文件
# public static void main(String[] args) {  
#         String msgSaveFile = countHtmlFilePath("c://", new Date()) + "/test.html";  
#         String messageHtmlCode = getHtmlCode("test.jsp", "UTF-8");  
#         saveHtmlCode(msgSaveFile, messageHtmlCode);  
#     }  
#       
#     // 获得存储文件的目录  
#     public final static String countHtmlFilePath(String url, Date date) {  
#         SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd-hh");  
#         String time = sim.format(date);  
#         String[] result = time.split("-");  
#         String year = result[0];  
#         String month = result[1];  
#         String day = result[2];  
#         String hour = result[3];   
#         // 首先判断是否有年文件夹  
#         MyFileFilter filter = new TestClass().new MyFileFilter(true);  
#         boolean isExists = filter.isExistsDirectory(url, year + "");  
#         if (isExists) {  
#             if(filter.isExistsDirectory(url + "/" + year, month + "")) {  
#                 if(filter.isExistsDirectory(url + "/" + year + "/" + month, day + "")) {  
#                     if(!filter.isExistsDirectory(url + "/" + year + "/" + month + "/" + hour, hour + "")) {  
#                         new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();  
#                     }  
#                 } else {  
#                     new File(url + "/" + year + "/" + month + "/" + day).mkdir();  
#                 }  
#             } else {  
#                 new File(url + "/" + year + "/" + month).mkdir();  
#             }  
#         } else {  
#             new File(url + "/" + year).mkdir();  
#             new File(url + "/" + year + "/" + month).mkdir();  
#             new File(url + "/" + year + "/" + month + "/" + day).mkdir();  
#             new File(url + "/" + year + "/" + month + "/" + day + "/" + hour).mkdir();  
#         }  
#         return url + "/" + year + "/" + month + "/" + day + "/" + hour;  
#     }  
#       
#   
#     class MyFileFilter implements java.io.FileFilter {  
#   
#         private boolean isDirectory;  
#   
#         public MyFileFilter(boolean isDir) {  
#             this.isDirectory = isDir;  
#         }  
#   
#         @Override  
#         public boolean accept(File f) {  
#             if (f.isDirectory() == isDirectory) {  
#                 return true;  
#             } else  
#                 return false;  
#         }  
#           
#         public boolean isExistsDirectory(String path, String dir) {  
#             File file = new File(path);  
#             File[] childFiles = file.listFiles(this);  
#             for (File cFile : childFiles) {  
#                 if(cFile.equals(dir)) {  
#                     return true;  
#                 }  
#             }  
#             return false;  
#         }  
#     } 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值