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

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

问题描述

使用java.net包中的URL类.

Using URL class in java.net package.

方法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类.

Using Apache's HttpClient class.

方法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下载空白图像.我已经阅读了两种方法,但是我不明白为什么method1下载正确的文件和method2下载错误的文件.请澄清这一点,并让我知道方法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.

推荐答案

首先:交叉发布:第二:我想问题是url,以及javas内部类和apache lib对URL的处理方式不同-使用调试器并逐步检查它们,以查看真正从tls流中发送出的url.

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天全站免登陆