[英]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 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: 问题:
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.