首页 > 解决方案 > 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!!!

标签: javahttpurl

解决方案


推荐阅读