Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28575100
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2009-10-20 11:10:31

1.想下载远程URL地址的内容。可以使用httpclient现在整理一下相关的代码:
而且解决中文乱码问题
方法一:流转码
public String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"gbk"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
           try {
            is.close();
           } catch (IOException e) {
            e.printStackTrace();
           }
      }
      return sb.toString();
     }
//下载内容
private String urlContent(String urlString) throws HttpException, IOException {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod("");
        client.executeMethod(get);
        System.out.print(get.getResponseCharSet());
        InputStream iStream = get.getResponseBodyAsStream();
        String contentString = convertStreamToString(iStream);
       
        get.releaseConnection();
        return contentString;
    }

通过 GET方法能够实现下载网页内容出来的
阅读(1085) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~