java - Java URL下载HTML内容而不是文件?
问题描述
我正在尝试使用 Java URL 类下载文件,但它正在下载 HTML 内容。
class DownloadFileHttpCilent {
public static void main(String[] args) throws Exception {
try {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(
"https://url");
String encoding=Base64.getEncoder().encodeToString(("abcd:pwd").getBytes());
request.setHeader("Authorization", "Basic " + encoding);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("Request Url: " + request.getURI());
System.out.println("Response Code: " + responseCode);
InputStream is = entity.getContent();
String filePath = "c:\\file1.zip";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while ((inByte = is.read()) != -1) {
fos.write(inByte);
}
is.close();
fos.close();
client.close();
System.out.println("File Download Completed!!!");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对于其他开源 URL,它工作正常,但仅在这种情况下,它受密码保护,它正在下载 HTML 内容。
输出:
Request Url: https://abcd.cahj.com/defj
Response Code: 200
File Download Completed!!!
解决方案
推荐阅读
- javascript - 使用 ASP.NET web api 2 上传 xml 格式的 Excel 文件
- postgresql - 执行 pg_dump 时出现共享内存错误
- shell - 通过 jenkins 中的 shell 脚本将 ipa 上传到 hockeyapp
- java - 使用 github 通用网络钩子触发 Jenkins
- c++ - 致命错误:cuda.h:C++ 没有这样的文件或目录
- apache-nifi - 我们可以从包含一个流的单个进程组中只提升单个流吗?
- string-formatting - 重复动作:开始时间和随后的频率格式约定
- r - 无法通过 RStudio 的任务调度程序启动浏览器
- php - 内爆关联数组
- hyperledger-fabric - 如何将 HyperLedger composer Business 网络与 Hyperledger explorer 集成?