Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java URL.openStream()和HttpResponse.getEntity().getContent()下载Inputstream的不同文件_Java_Http_Httpclient_Inputstream_Apache Httpclient 4.x - Fatal编程技术网

Java URL.openStream()和HttpResponse.getEntity().getContent()下载Inputstream的不同文件

Java URL.openStream()和HttpResponse.getEntity().getContent()下载Inputstream的不同文件,java,http,httpclient,inputstream,apache-httpclient-4.x,Java,Http,Httpclient,Inputstream,Apache Httpclient 4.x,在java.net包中使用URL类 方法1 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&ve

在java.net包中使用URL类

方法1

        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类

方法2

        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中是否有修复程序,我可以通过它下载正确的文件。

第一:交叉发布:

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

我删除了标记(两次),因为这个问题与apache httpd无关。在再次编辑此文件之前,请阅读标签的说明,其目的非常清楚。