网页中有这样一段代码现实了一张图片<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();
我该如何通过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();
解决方案 »
- 关于java算法的疑惑
- Java中GUI,我想把咖啡图标去掉,求方法……
- 谁闲着没事干的,帮我看下这个问题
- HttpURLConnection 类中setRequestProperty()的使用
- 为什么我的按钮响应处理函数总是被调用两次?希望有详解
- JAVA怎样才能学好,学好了能用它吃饭吗?
- jacob如何写ppt想实现的功能页面在 <http://blog.163.com/china_os2001/blog/static/503380232007102631941853/,谢谢大家了!
- 求救关于ResultSet的问题,急!解决立即结贴!
- javaApplet是否不能加菜单?如果可以怎么加?
- 请大家帮忙
- 微软应聘的笔试题,哪位大拿帮忙做一下
- Java题目2(帮我看看哪些是不对的)
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,