输入流read是堵塞线程的,而socket的流并没有结束标记,所以你用-1的标记是无法识别的,只会一直堵塞,可以参考https://github.com/wkp111/SocketSimple
断点到哪步下不去了,执行到哪里不能继续执行了,有报错吗
楼上说的对,读完了返回值不是-1,你这样就是一直在重复的读取这个文件的前1024个字节。
public static void receiveFile(Socket socket) {
byte[] inputByte = null;
int length = 0;
DataInputStream dis = null;
FileOutputStream fos = null;
try {
try {
dis = new DataInputStream(socket.getInputStream());
fos = new FileOutputStream(new File("E:\img22.jpg"));
inputByte = new byte[1024 * 4];
System.out.println("开始接收数据...");
while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
fos.write(inputByte, 0, length);
fos.flush();
}
System.out.println("完成接收");
} finally {
if (fos != null)
fos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
}
}
我是这么写的!