編輯:關於Android編程
本文實例講述了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程序設計有所幫助。
照片牆這種功能現在應該算是挺常見了,在很多應用中你都可以經常看到照片牆的身影。它的設計思路其實也非常簡單,用一個GridView控件當作“牆”,然
有很多人喜歡在手機上下東西,但是有手機在休眠的時候WIFI會斷網,這樣想下載的東西也下載不了了,那麼怎麼避免這種情況出現呢,下面小編就給大家介紹下防止手機休
項目介紹 因為要參加某信息安全比賽,選擇了安卓apk的行為分析與評估的課題,所以首先需要了解安卓程序是怎樣編寫和運行的。我們的第一個任務就是寫出一個多人通信的app。
先來看看網易新聞客戶端以及自己實現的效果圖,效果當然還是網易的好gridviewsort.gif如何實現拖拽一個Item用WindowManager添加一個ImageVi