1

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();
3
  • Try changing "> 0" in the while loop to != -1. It might be reading lots of zero-length arrays but hasn't actually finished yet (you get a -1 when the stream has ended). I always prefer to use something like HttpClient to do things like this (as it handles things like redirects etc), maybe look at this thread... stackoverflow.com/questions/10960409/…
    – BretC
    Mar 30, 2015 at 15:44
  • Pshemo yes I am, sorry I couldn't specify it in the code. Mar 30, 2015 at 15:48
  • Bret thanks for the suggestion, but it didn't work. I'll check the question that you have shared thanks. mean time if you could find some thine more please let me know. Thanks a lot Mar 30, 2015 at 15:51

1 Answer 1

1

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...

3
  • Oh I never open it. Clever you have done that. Actually I'm not sure which header to change, and how to do that. Anyway as you have suggested I'm going to use HttpClient, hope it will work. Mar 30, 2015 at 16:10
  • @Shan-mobile I suspect you actually want to be downloading HTTPS>>"avisloyalty.eu/assets/fleetlarge//VW_Passat_15.jpg", but when I try that, I get a load of certificate errors - you might want to look at stackoverflow.com/questions/1828775/…
    – BretC
    Mar 30, 2015 at 16:17
  • Yes, exactly I was working on the same issue too. Just trying to put their certificate to ssl-keystore. Anyway thanks a lot for helping me. I hope I can manage from here. Mar 30, 2015 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.