概述
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public static String getFile() {
String userName = "";
if (getSeeyonRestClient()) {
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClients.createDefault();
String url = 地址;
HttpGet httpGet = new HttpGet(url);
// 发起请求 并返回请求的响应
CloseableHttpResponse response = httpClient.execute(httpGet);
System.out.println("response: "+response);
try {
// 获取响应对象
HttpEntity resEntity = response.getEntity();
System.out.println("resEntity: "+resEntity);
byte[] data = EntityUtils.toByteArray(resEntity);
System.out.println(data);
FileOutputStream fos = new FileOutputStream("D:\Workspace\aa.pdf");
fos.write(data);
fos.close();
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return userName;
}
最后
以上就是复杂小白菜为你收集整理的HttpClients下载文件的全部内容,希望文章能够帮你解决HttpClients下载文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复