图像部分下载,

时间:2015-03-30 15:38:11

标签: java image http file-transfer

我无法在以下网址下载该文件:http://avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg

仅下载文件的一部分。

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

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();

1 个答案:

答案 0 :(得分:1)

我刚试过这个......

    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();

...并且输出文件包含TEXT ...

<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而不是&gt; 0)!也许您需要设置请求标题或其他内容......