Android通過SOCKET下載文件的方法
本文實(shí)例講述了Android通過SOCKET下載文件的方法。分享給大家供大家參考,具體如下:
服務(wù)端代碼
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();//創(chuàng)建線程
}
}
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);
//根據(jù)路徑和文件名獲取文件
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());//發(fā)送文件大小
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 = "服務(wù)器IP";
int socketPort = "服務(wù)端口號(hào)";
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(); // 清理緩沖,確保發(fā)送到服務(wù)端
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();
}
}
}
}
基本可以直接用,根據(jù)自己需要稍微改動(dòng)就OK了
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼
- Android中使用AsyncTask實(shí)現(xiàn)下載文件動(dòng)態(tài)更新進(jìn)度條功能
- android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條
- android實(shí)現(xiàn)多線程下載文件(支持暫停、取消、斷點(diǎn)續(xù)傳)
- Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
- 使用Android系統(tǒng)提供的DownloadManager來下載文件
- Android實(shí)現(xiàn)多線程下載文件的方法
- Android實(shí)現(xiàn)下載文件功能的方法
- Android使用Handler實(shí)現(xiàn)下載文件功能
相關(guān)文章
Android 運(yùn)用@JvmName解決函數(shù)簽名沖突問題詳解
JvmName注解是Kotlin提供的一個(gè)可以變更編譯器輸出的注解,這里簡(jiǎn)單的介紹一下其使用規(guī)則,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-07-07
Kotlin 使用Lambda來設(shè)置回調(diào)的操作
這篇文章主要介紹了Kotlin 使用Lambda來設(shè)置回調(diào)的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
使用Android WebSocket實(shí)現(xiàn)即時(shí)通訊功能
即時(shí)通訊(Instant Messaging)最重要的毫無疑問就是即時(shí),不能有明顯的延遲,要實(shí)現(xiàn)IM的功能其實(shí)并不難,目前有很多第三方,比如極光的JMessage,都比較容易實(shí)現(xiàn)。本文通過實(shí)例代碼給大家分享Android WebSocket實(shí)現(xiàn)即時(shí)通訊功能,一起看看吧2019-10-10
Android 從底部彈出Dialog(橫向滿屏)的實(shí)例代碼
在android開發(fā)中經(jīng)常會(huì)遇到底部彈出框的功能,今天小編抽時(shí)間給大家整理一個(gè)底部彈出橫向滿屏的dialog,需要的朋友參考下2016-11-11
Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼
這篇文章主要介紹了Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Flutter 底部彈窗如何實(shí)現(xiàn)多項(xiàng)選擇
在Flutter中提供了一個(gè)showModelBottomSheet方法用于彈出底部彈窗,本篇基于這個(gè)方法介紹實(shí)現(xiàn)底部彈窗多選的思路和方式。2021-06-06

