版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28779083/article/details/80027628
1、Socket文件上传演示场景:一个手机作为发送端、一个手机作为接收端、两部手机在同一wifi网络环境下。
github源码地址,可以下载对照着看。
2、首先用一部手机来创建并启动接收端的Socket服务。部分代码:
- 1、创建服务端ServerSocket,接收文本信息。
/**
* 创建服务端ServerSocket
* 接收文本消息
*/
private void createStringServerSocket() {
Runnable run = new Runnable() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.clear();
OutputStream out;
//给发送端返回一个消息,告诉他链接接收成功。
String str = "200";
try {
ServerSocket serverSocket = new ServerSocket(StringProt);
while (true) {
try {
//此处是线程阻塞的,所以需要在子线程中
Socket socket = serverSocket.accept();
//请求成功,响应客户端的请求
out = socket.getOutputStream();
out.write(str.getBytes("utf-8"));
out.flush();
socket.shutdownOutput();
//获取输入流,读取客户端发送来的文本消息
BufferedReader bff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = bff.readLine()) != null) {
buffer.append(line);
}
buffer.append("\n");
//
handler.sendEmptyMessage(1);
bff.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
executorService.execute(run);
}
- 2、创建服务端ServerSocket,接收文件。
/**
* 创建服务端ServerSocket
* 接收文件
*/
private void createFileServerSocket() {
Runnable run = new Runnable() {
@Override
public void run() {
//给发送端返回一个消息,告诉他链接接收成功。
String str = "200";
try {
ServerSocket serverSocket = new ServerSocket(FileProt);
while (true) {
try {
//此处是线程阻塞的,所以需要在子线程中
Socket socket = serverSocket.accept();
//请求成功,响应客户端的请求
OutputStream out = socket.getOutputStream();
out.write(str.getBytes("utf-8"));
out.flush();
socket.shutdownOutput();
//获取输入流,读取客户端发送来的文件
DataInputStream dis = new DataInputStream(socket.getInputStream());
// 文件名和长度
String fileName = dis.readUTF();
//接收到的文件要存储的位置
File directory = new File(sdPath + "/imgage/");
if (!directory.exists()) {
directory.mkdirs();
}
FileOutputStream fos = new FileOutputStream(directory.getAbsolutePath() + "/" + fileName);
// 开始接收文件
byte[] bytes = new byte[1024];
int length = 0;
while ((length = dis.read(bytes, 0, bytes.length)) != -1) {
fos.write(bytes, 0, length);
fos.flush();
}
dis.close();
fos.close();
out.close();
socket.close();
handler.sendEmptyMessage(3);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
executorService.execute(run);
}
3、用另一部手机,再来创建并启动发送端的Socket。部分代码:
- 1、启动线程 向服务器发送文本消息
/**
* 启动线程 向服务器发送文本消息
*/
private void sendStringMessage(final String txt1, final String ip) {
Runnable run = new Runnable() {
@Override
public void run() {
try {
Socket socket = new Socket();
//端口号为StringProt
socket.connect(new InetSocketAddress(ip, StringProt));
//获取输出流
OutputStream ou = socket.getOutputStream();
//读取服务器响应
BufferedReader bff = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String line = null;
String buffer = "";
while ((line = bff.readLine()) != null) {
buffer = line + buffer;
}
//向服务器发送文本信息
ou.write(txt1.getBytes("utf-8"));
//关闭各种输入输出流
ou.flush();
bff.close();
ou.close();
socket.close();
//服务器返回
Message message = new Message();
message.what = 2;
message.obj = buffer;
handler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
};
executorService.execute(run);
}
- 2、启动线程 向服务器发送文件
/**
* 启动线程 向服务器发送文件
*/
private void sendFileMessage(final String fliePath, final String ip) {
Runnable run = new Runnable() {
@Override
public void run() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, FileProt));
//获取输出流
OutputStream ou = socket.getOutputStream();
//读取服务器响应
BufferedReader bff = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String line = null;
String buffer = "";
while ((line = bff.readLine()) != null) {
buffer = line + buffer;
}
//向服务器发送文件
File file = new File(fliePath);
if (file.exists()) {
FileInputStream fileInput = new FileInputStream(fliePath);
DataOutputStream dos = new DataOutputStream(ou);
// 文件名
dos.writeUTF(file.getName());
byte[] bytes = new byte[1024];
int length = 0;
while ((length = fileInput.read(bytes)) != -1) {
dos.write(bytes, 0, length);
}
//告诉服务端,文件已传输完毕
socket.shutdownOutput();
fileInput.close();
dos.close();
//服务器返回码
Message message = new Message();
message.what = 2;
message.obj = buffer;
handler.sendMessage(message);
}
//关闭各种输入输出流
ou.flush();
bff.close();
ou.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
executorService.execute(run);
}