Transferring Data with Java Networking
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jack_Wilson
Posted On:   Sunday, April 14, 2002 01:34 PM

Hi all. I'm new to Java programming, so if my question is rather simple, please forgive me. Anyway, I've been trying to transfer files from a web server to my hard drive via a Java application. I've used both sockets and the URL class, but in both cases, the sound files I keep trying to download become garbled, or in some cases won't play at all. I know I could just download the files through my browser and be done with it, but this was more of an academic thing to get a feel for Java, so I was hoping some of you could enlighten me. If you need a look at my code to give me an answer, just ask and I'll post it. Thank you.

Regards,
Jack Wilson

Re: Transferring Data with Java Networking

Posted By:   Terry_Laurenzo  
Posted On:   Sunday, April 14, 2002 04:23 PM

I would need to see your code to tell you why they are getting garbled. If you are doing it right, they should come across correctly. However, from experience with people having these types of problems, I would guess that you are using a Reader instead of an InputStream. In any case, the following should work:


URL url=new URL("http://somewhere.com/mysoundfile.wav");
InputStream in=url.openStream();
OutputStream out=new FileOutputStream("mysoundfile.wav");
byte[] buffer=new byte[4096];

int r;
for (;;) {
r=in.read(buffer);
if (r==0) continue;
if (r==-1) break;

out.write(buffer,0,r);
}

out.close();
in.close();
About | Sitemap | Contact