这是我现在所拥有的。通过流发送文件
接收机:
public static void read(Socket socket, ObjectInputStream in) {
try {
String separator = in.readUTF();
while (in.readByte() == -3) {
String path = in.readUTF().replaceAll(separator, System.getProperty("file.separator"));
File file = new File(new File(path).getParent());
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(path);
int b = 0;
while ((b = in.readByte()) != -4) {
fos.write(b);
}
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
发件人:
public static void send(String[] path) {
Socket socket;
try {
socket = new Socket(ip, port);
socket.setKeepAlive(true);
} catch (UnknownHostException e) {
return;
} catch (IOException e) {
return;
}
try {
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeUTF(Devbox.getSeparator());
for (String s : path) {
send(s, out);
out.writeByte(-2);
}
out.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void send(String path, ObjectOutputStream out) {
File file = new File(path);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
send(path + f.getName(), out);
}
} else {
try {
out.writeByte(-3);
out.writeUTF(path);
FileInputStream fis = new FileInputStream(file);
int b = 0;
while ((b = fis.read()) != -1) {
out.writeByte(b);
}
fis.close();
out.writeByte(-4);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我在发送方得到的错误。
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeBlockHeader(ObjectOutputStream.java:1874)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1855)
at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1895)
at java.io.ObjectOutputStream.writeByte(ObjectOutputStream.java:760)
它指向
out.writeByte(b);
它成功地将约25文件,则引发此错误。它每次都抛出它的文件不同,但它在大约5个文件的相同范围内。接收器在发送者停止之前通常是一对特定文件之后停止。它停止,因为in.readByte()== -3为false。当它发生时,我得到了像-85和16这样的数字。我在另一台计算机上试了一下,因为它说了一些关于软件的内容,而且它完全一样。有谁知道为什么会发生这种情况?我花了一天的时间试图弄明白,并没有得到任何答案。任何帮助是极大的赞赏。
你能保证你的输入文件不包含值-4吗?否则,接收方停止将字节写入输出文件(甚至可能关闭连接?),而发送方仍在向流写入数据。 – 2012-04-24 16:00:03
@AndreHolzner是的,我可以保证这不是问题。 – Stripies 2012-04-24 16:10:26