• Post Reply Bookmark Topic Watch Topic
  • New Topic

Download objects(picts, mp3, etc) with URL class  RSS feed

 
Mike Li
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I've this segment of code that downloads objects from any web sites and store to local files..
.....
URL v_url = new URL(urlBase + "/" + urlFile);
URLConnection v_urlc = v_url.openConnection();
InputStream v_in = v_urlc.getInputStream() ;
.....
v_outf = new FileOutputStream (localFile);
int inChr ;
while ( (inChr = v_in.read()) >= 0)
v_outf.write( inChr );
.......
It works sometimes without issues, but some other times it just downloaded part of the object. How do I make it ensure that it downloads the complete object everytime?
Thanks!
Mike.
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you close() the output stream - it might have some stuff buffered that's not making it to the file. Closing the stream will ensure it makes it out.
 
Mike Li
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave, but I did have 2 stmts right after that segment to close both streams...still it's somehow being inconsistent..
v_in.close();
v_outf.close();
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*scratches head*
Looks like it ought to work. Are any exceptions being thrown?
- Peter
 
Ray Lehman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yours sounds a lot like a problem I've been having. I don't have a working solution yet, but I'm beginning to think it has something to do with http headers. I've read that some servers will not send back a file (particulary images, mp3's, etc...) if a properly formed "GET" header isn't sent.
I'm really new to this stuff so that may not be the case either. I was wondering if the URLConnection class automatically sent the proper HTTP headers. I don't think so, but I would not be surprised if I were wrong.
Now, for the sake of argument let's assume that the server is denying access because the "GET" header isn't being sent. Would the proper approach be to use sockets and send the header as a string? What about getting the response... i.e. will the server automatically send its response header and the file over the same socket?
I'm sorry for adding more questions to the original thread, but this is something I've been wondering about for a couple of days.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The GET is not really a header field, it's the command (method) being sent to the server. It has to be the very first line of the request. For example, when you retrieve the URL "http://www.javaranch.com/name.jsp", your browser will contact the host "www.javaranch.com" on the HTTP port (80), and send it a request starting with
GET /name.jsp HTTP/1.1
There isn't an awful lot to get wrong there. The case of your filename perhaps (Windows is case insensitive, but URLs paths are not).
There is no way to do this yourself unless you ditch the URL class completely and do your own thing. Can you be more specific about the problems you're having?
- Peter
 
Yongping Wang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think your code works for you if the target source is protected by password or something like that.
In this case, you have to use setRequestProperty("Authorization", "Basic xxxxxxx");
Y.Wang
[ December 13, 2002: Message edited by: Yongping Wang ]
 
Ray Lehman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
99% of the time my code (which is basically the same as the previous author's) downloads the object just fine. On some servers it winds up downloading an html file instead of a binary. I was just thinking that a possibility may be that the server is set up so that if a proper GET method isn't sent (i.e. you're not going through a web browser), then the server will send back the html file instead of the requested binary. I've heard some servers are set up this way to prevent automatic downloads.
Right now I'm more in the "thinking about it" stage. It is not a pressing matter, I was just wondering that if the server is doing what is described above if the URLConnection class should be replaced with a raw socket in which I could send the GET request myself. In other words, open a socket and simulate the actions of a web browser. If that's the case then I know the server would reply with its own response header and then the binary file. I'm guessing this would automatically come across the same socket?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!