Android通过SOCKET下载文件的方法
程序员文章站
2023-12-17 14:42:16
本文实例讲述了android通过socket下载文件的方法。分享给大家供大家参考,具体如下:
服务端代码
import java.io.bufferedinpu...
本文实例讲述了android通过socket下载文件的方法。分享给大家供大家参考,具体如下:
服务端代码
import java.io.bufferedinputstream; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.serversocket; import java.net.socket; import java.util.scanner; public class functionserver { private static int port = 2012; private string path = "需要下载的文件所在路径"; public static void main(string[] args) throws ioexception{ functionserver server = new functionserver(); server.start(); } public void start() throws ioexception{ serversocket ss = new serversocket(port); while(true){ socket s = ss.accept(); new service(s).start();//创建线程 } } class service extends thread{ socket s; public service(socket s){ this.s = s; } public void run(){ try{ inputstream in = s.getinputstream();//得到输入流 scanner sc = new scanner(in); outputstream out = s.getoutputstream(); while(true){ string str = sc.nextline();//读取文件名 if(!str.equals(null)){ system.out.println("你的文件名是"+str); //根据路径和文件名获取文件 file f = new file(path+str); fileinputstream fis = new fileinputstream(f); datainputstream dis = new datainputstream(new bufferedinputstream(fis)); byte[] buffer = new byte[8192]; dataoutputstream ps = new dataoutputstream(out); ps.writelong((long) f.length());//发送文件大小 ps.flush(); while(true) { int read = 0; if(dis!=null){ read = fis.read(buffer); } if(read == -1){ break; } ps.write(buffer,0,read); } ps.flush(); dis.close(); s.close(); out.flush(); break; } } }catch(ioexception e){ e.printstacktrace(); } } } }
客户端代码,下载线程
class downloadthread extends thread { socket socket; inputstream in; outputstream out; string path = "文件保存路径"; string functionname; string serverip = "服务器ip"; int socketport = "服务端口号"; int filesize,downloadfilesize; public downloadthread(string functionname) { this.functionname = functionname; } @override public void run() { looper.prepare(); while(!thread.interrupted()){ try { socket = new socket(serverip, socketport); inputstream in = socket.getinputstream(); outputstream out = socket.getoutputstream(); out.write((functionname + "\n").getbytes("gbk")); out.flush(); // 清理缓冲,确保发送到服务端 file f = new file(path + functionname); outputstream song = new fileoutputstream(f); datainputstream dis = new datainputstream( new bufferedinputstream(in)); dataoutputstream dos = new dataoutputstream( new bufferedoutputstream(song)); filesize = (int) dis.readlong() - 1; system.out.println("开始下载"); byte[] buffer = new byte[8192]; while (true) { int read = 0; if (dis != null) { read = dis.read(buffer); downloadfilesize += read; } if (read == -1) { break; } dos.write(buffer, 0, read); } system.out.println("文件下载完成"); dos.close(); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { this.interrupt(); } } } }
基本可以直接用,根据自己需要稍微改动就ok了
希望本文所述对大家android程序设计有所帮助。