简体   繁体   English

如何从服务器下载文件

[英]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. 我想做一个基于android客户端和java服务器的应用程序。 Local wifi transmission, no 3G. 本地wifi传输,没有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 must create a background thread in the client to create a file in the SD card and update a progress bar using a Handler to communicate with the UI thread. 我必须在客户端创建一个后台线程,以在SD卡中创建一个文件,并使用Handler更新进度条以与UI线程进行通信。

  • The server must be multithread and non-blocking. 服务器必须是多线程且无阻塞的。

  • The file is a binary file like a mp3 audio. 该文件是一个二进制文件,如mp3音频。 So the server has to: 所以服务器必须:

    1. Send information about the file: name and total length. 发送有关文件的信息:名称和总长度。
    2. Open the file, read and send bytes while it does not reach the end. 打开文件,读取并发送字节,直到它没有到达结尾。

  • The client has to: 客户必须:

    1. Receive the information about the file and create an empty file. 接收有关文件的信息并创建一个空文件。
    2. Read bytes and dump them into the empty file. 读取字节并将其转储到空文件中。 Update progress bar. 更新进度条。
    3. When all bytes are recieved close the file. 收到所有字节后关闭文件。

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. 我知道在C中实现客户端和服务器(非常糟糕)但我开始使用java完成的真正的客户端 - 服务器应用程序。

Questions: 问题:

  • How can I download a binary file like an mp3 from a server to a client? 如何从服务器下载像mp3这样的二进制文件到客户端?

  • Where I have to put my server application? 我必须把我的服务器应用程序放在哪里? I supose that I must create a jar, save it on a folder and execute it at PC start-up, right? 我想我必须创建一个jar,将它保存在一个文件夹中并在PC启动时执行它,对吗?

Thanks! 谢谢!

How can I download a binary file like an mp3 from a server to a client? 如何从服务器下载像mp3这样的二进制文件到客户端?

To download a file with Java, you can Use URL.openStream(); 要使用Java下载文件,可以使用URL.openStream();

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

Don't print the output to System.out . 不要将输出打印到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). 如果可能,使用http-Server(Tomcat或Apache HTTPD)。 Make your file available through HTTP. 通过HTTP使您的文件可用。

If you want to use a Java Server, you should write a Servlet and packkage it into a WAR -File: 如果要使用Java Server,则应编写Servlet并将其打包到WAR -File中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM