Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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 图像正在部分下载,_Java_Image_Http_File Transfer - Fatal编程技术网

Java 图像正在部分下载,

Java 图像正在部分下载,,java,image,http,file-transfer,Java,Image,Http,File Transfer,我无法下载以下url中的文件: 仅下载文件的一部分 下面是我的代码。如果我做错了什么,请告诉我 URLConnection urlConn = new URL( "http://avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg").openConnection(); InputStream is = urlConn.getInputStream(); FileOutputStream fos = new FileOutputStream( fi

我无法下载以下url中的文件:

仅下载文件的一部分

下面是我的代码。如果我做错了什么,请告诉我

URLConnection urlConn = new URL( "http://avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg").openConnection();
InputStream is = urlConn.getInputStream();
FileOutputStream fos = new FileOutputStream( file.getPath() );

byte[] buffer = new byte[4096];
int len;

while( ( len = is.read( buffer ) ) > 0 )
{
    fos.write( buffer, 0, len );
}
fos.close();
我刚试过这个

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet("http://avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg");

    HttpResponse execution = client.execute(get);
    HttpEntity entity = execution.getEntity();
    FileOutputStream outputStream = new FileOutputStream("C:\\tmp\\imgout.jpg");

    if (entity != null) {
        InputStream inputStream = entity.getContent();
        IOUtils.copy(inputStream, outputStream);
    }

    outputStream.close();
…并且输出文件包含文本

<HTML>
<HEAD>
<TITLE>avisloyalty.eu</TITLE>
<META NAME="robots" CONTENT="noindex">
</HEAD>
<FRAMESET FRAMESPACING="0" BORDER="0" FRAMEBORDER=No ROWS="100%,*">
  <FRAME SRC="https://www.avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg">
</FRAMESET>
<NOFRAMES>
Sorry, your browser doesn't seem to support frames! <br>
Proceed to <A href="https://www.avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg">https://www.avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg</A> manually.
</NOFRAMES>
</HTML>

忠诚
很抱歉,您的浏览器似乎不支持框架
继续手动操作。

因此,您的代码可能没有问题(我仍然使用!=-1而不是>0)!也许您需要设置一个请求头或其他东西…

尝试将while循环中的“>0”更改为!=-1.它可能正在读取大量的零长度数组,但实际上还没有完成(当流结束时,会得到-1)。我总是喜欢使用像HttpClient这样的东西来做这样的事情(因为它处理重定向等事情),也许看看这个线程。。。Pshemo是的,很抱歉,我无法在代码中指定它。Bret感谢您的建议,但它不起作用。我会检查你分享的问题谢谢。同时如果你能找到更多的,请告诉我。非常感谢,我从来没打开过。你这么做真聪明。实际上,我不确定该更改哪个标题,以及如何更改。不管怎样,正如您建议我使用HttpClient,希望它能起作用。@Shan mobile我怀疑您确实想下载HTTPS>>”,但当我尝试时,我收到了大量证书错误-您可能想看看是的,我也在处理同一问题。只是尝试将他们的证书放入ssl密钥库。无论如何,非常感谢你帮助我。我希望我能从这里开始。