接受文件的线程:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Sthread implements Runnable{
ServerSocket server = null;
@Override
public void run() {
// TODO Auto-generated method stub
try {
server = new ServerSocket(33456);
while (true) {
try {
System.out.println("开始监听。。。");
Socket socket = server.accept();
System.out.println("有链接");
receiveFile(socket);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void receiveFile(Socket socket) throws IOException {
byte[] inputByte = null;
int length = 0;
DataInputStream din = null;
FileOutputStream fout = null;
String savePath; // 文件保存路径
try {
// 判断接收文件的文件夹是否存在,若不存在,则创建
savePath = "/mnt/sdcard/atest2/";
File fileDir = new File(savePath);
if (!fileDir.exists()) { // 若不存在
fileDir.mkdir();
}
din = new DataInputStream(socket.getInputStream());
// fout = new FileOutputStream(new File("/sdcard2/DCIM/Camera/"+din.readUTF()));
if (new File(savePath+din.readUTF()).exists()) { // 若对应文件名的文件已存在,则删除原来的文件
new File(savePath+din.readUTF()).delete();
}
fout = new FileOutputStream(new File(savePath+din.readUTF()));
inputByte = new byte[1024];
System.out.println("开始接收数据...");
while (true) {
if (din != null) {
length = din.read(inputByte, 0, inputByte.length);
}
if (length == -1) {
break;
}
System.out.println(length);
fout.write(inputByte, 0, length);
fout.flush();
}
System.out.println("接收完成");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (fout != null)
fout.close();
if (din != null)
din.close();
if (socket != null)
socket.close();
}
}
}
发送文件的线程:
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import android.util.Log;
public class Cthread implements Runnable{
int length = 0;
byte[] sendByte = null;
Socket socket = null;
DataOutputStream dout = null;
FileInputStream fin = null;
String pathString = null;
String IP = null;
public String getIP() {
return IP;
}
public void setIP(String iP) {
IP = iP;
}
public String getPathString() {
return pathString;
}
public void setPathString(String pathString) {
this.pathString = pathString;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("客户端向本机发起连接");
socket = new Socket();
try {
socket.connect(new InetSocketAddress("255.255.255.255", 33456),10 * 1000);
dout = new DataOutputStream(socket.getOutputStream());
File file = new File(pathString);
fin = new FileInputStream(file);
sendByte = new byte[1024];
dout.writeUTF(file.getName());
while((length = fin.read(sendByte, 0, sendByte.length))>0){
dout.write(sendByte,0,length);
dout.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (dout != null)
try {
dout.close();
if (fin != null)
fin.close();
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}