這是我現在所擁有的。通過流發送文件
接收機:
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