81,094
社区成员
发帖
与我相关
我的任务
分享
URL url = new URL("某个网址");
HttpsURLConnection urlcon = (HttpsURLConnection) url.openConnection();
urlcon.setRequestMethod("GET");//POST也试过
urlcon.connect();
InputStream in=urlcon.getInputStream();
fos = new FileOutputStream("../LoginXml/a.xml");
while ( (size = in.read(buf)) != -1)
{
fos.write(buf, 0, size);
}
fos.flush();
fos.close();
PostMethod post = new PostMethod(url);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
client.executeMethod(post);
Header[] heads = post.getResponseHeaders();
boolean isword = false;
for(Header h : heads){
String n = h.getName();
if(n.equalsIgnoreCase("Content-Type")){
String v = h.getValue();
if(v.indexOf("application") >= 0 || v.indexOf("word") >= 0){
//下载到的资源是图片
isword = true;
break;
}
}
}
boolean success = isword;
System.out.println(" success :" + success);
if(success){
FileOutputStream out = new FileOutputStream(file);
out.write(post.getResponseBody());
out.close();
return file;
}else{
return null;
}
给你一个代码片段,目的是下载一个Word文档