Gabriel Llamas
Gabriel Llamas

Reputation: 18427

How to download a file from a server

I have a question with a general design implementation. Hope anyone more skilled than me helps me.

I want to do an application based on an android client and a java server. Local wifi transmission, no 3G.

Basically, the client must connect to the server and request a file to download using a code.

How can I do that?

Things I know:

I have knowledge implementing a client and server in C (very awful) but I am beginning with a real client-server application done in java.

Questions:

Thanks!

Upvotes: 3

Views: 3433

Answers (1)

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

How can I download a binary file like an mp3 from a server to a client?

To download a file with Java, you can Use URL.openStream();

http://download.oracle.com/javase/tutorial/networking/urls/readingURL.html

Don't print the output to System.out. Write it to a file, instead.

FileOutputStream fos = new FileOutputStreamm(new File("path_to_file.mp3");
int byte;

while ((byte= in.readLine()) != -1)
    fos.write(byte);

Where I have to put my server application? Don't implement a server unless you really have to. Use an http-Server if possile (Tomcat oder Apache HTTPD). Make your file available through HTTP.

If you want to use a Java Server, you should write a Servlet and packkage it into a WAR-File:

http://docstore.mik.ua/orelly/java-ent/servlet/

Upvotes: 5

Related Questions