Reputation: 699
I cannot download the file in following url: http://avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg
Only a part of the file is being downloaded.
Following is my code. Please let me know if I'm doing anything wrong.
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();
Upvotes: 1
Views: 147
Reputation: 4199
I just tried this...
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();
...and the output file contains the 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>
So probably nothing wrong with your code (I'd still use != -1 instead of > 0 though)! Maybe you need to set a request header or something...
Upvotes: 1