网页中有这样一段代码现实了一张图片<img border="0" Align="center" src="image.do" />
我该如何通过httpclient类获取这张图片呢?我试过下面这个方法但是获取的图片打不开,我怀疑获取的根本就不是图片,而是用来生成图片的程序,求大神指点
                  HttpClient client = new HttpClient();
                  GetMethod get = new GetMethod(
"http://xk.fudan.edu.cn/xk/img.do");
client.executeMethod(get);
File storeFile = new File("d:/sss.bmp");
FileOutputStream output = new FileOutputStream(storeFile);
// 得到网络资源的字节数组,并写入文件

output.write(get.getResponseBody());
output.close();
get.releaseConnection();

解决方案 »

  1.   

    package com.catchimage;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;public class CatchImage {
    private static HttpServletRequest req; public static HttpServletRequest getReq() {
    return req;
    }
    private static String rootAddress = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"; @SuppressWarnings("deprecation")
    public static void main(String[] args) {
    HttpClient httpClient = new HttpClient();
    httpClient.setConnectionTimeout(5000);
    httpClient.setTimeout(5000);
    GetMethod getMethod = new GetMethod(rootAddress);
    getMethod.getParams().setContentCharset("UTF-8");  
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler());
    try {
    int statusCode = httpClient.executeMethod(getMethod);
    if (statusCode == HttpStatus.SC_OK) {
        File storeFile = new File("d:/google.png");
        FileOutputStream output = new FileOutputStream(storeFile);
        output.write(getMethod.getResponseBody());
        output.close();
    }
    } catch (HttpException e) {
    e.printStackTrace();
    } catch (IOException e1) {
    e1.printStackTrace();
    } finally {
    getMethod.releaseConnection();
    }
    }
    }
    需要commons-codec-1.4.jar,commons-httpclient-3.1.jar,servlet-api-2.5.jar,
      

  2.   

    我是抓取了google首页的头图,然后写到本地google.png中去,你跑下试试