URL.openStream() 和 HttpResponse.getEntity().getContent() 下载 Inputstream 的不同文件 [英] URL.openStream() and HttpResponse.getEntity().getContent() downloading different files of Inputstream

查看:55
本文介绍了URL.openStream() 和 HttpResponse.getEntity().getContent() 下载 Inputstream 的不同文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 java.net 包中的 URL 类.

Using URL class in java.net package.

方法一

        String sourceUrl = "https://thumbor.thedailymeal.com/P09kUdGYdBReFSJne1qjVDIphDM=//https://videodam-assets.thedailymeal.com/filestore/5/3/0/2_37ec80e4c368169/5302scr_43fcce37a98877f.jpg%3Fv=2020-03-16+21%3A06%3A42&version=0";
        java.net.URL url = new URL(sourceUrl);
        InputStream inputStream = url.openStream();
        Files.copy(inputStream, Paths.get("/Users/test/rr.png"), StandardCopyOption.REPLACE_EXISTING);

使用 Apache 的 HttpClient 类.

Using Apache's HttpClient class.

方法二

        String sourceUrl = "https://thumbor.thedailymeal.com/P09kUdGYdBReFSJne1qjVDIphDM=//https://videodam-assets.thedailymeal.com/filestore/5/3/0/2_37ec80e4c368169/5302scr_43fcce37a98877f.jpg%3Fv=2020-03-16+21%3A06%3A42&version=0";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(sourceUrl);
        HttpResponse httpresponse = httpclient.execute(httpget);
        InputStream inputStream = httpresponse.getEntity().getContent();
        Files.copy(inputStream, Paths.get("/Users/test/rr.png"), StandardCopyOption.REPLACE_EXISTING);

我已经使用这两种方法下载了 rr.png 文件.我发现这两个文件的大小也不同,并且使用方法 2 下载了一个空白图像.我读过这两种方法是一样的,但我不明白为什么方法 1 下载正确的文件和方法 2 下载错误的文件.请澄清这一点,并让我知道方法 2 中是否有修复程序,我可以通过该修复程序下载正确的文件.

I have downloaded the rr.png file using both the methods. I found both the files are different even in sizes also and using method 2 download a blank image. I have read both the methods are same but I do not understand why method1 downloading correct file and method2 downloading wrong file. Please clarify this and also let me know if there is a fix in the method 2 through which I can download the correct file.

推荐答案

第一:交叉发布:https://coderanch.com/t/728266/java/URL-openStream-HttpResponse-getEntity-getContent

第二:我想问题是 url 以及 javas 内部类和 apache lib 如何以不同的方式处理它 - 使用调试器并逐步查看它们以查看 url 真正发送到 tls 流.

Second: I guess the issue is the url and how it's handled differently by javas internal class and apache lib - use a debugger and step through them to see what url really gets send out the tls stream.

这篇关于URL.openStream() 和 HttpResponse.getEntity().getContent() 下载 Inputstream 的不同文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆