gpt4 book ai didi

java - 通过流发送文件

转载 作者:行者123 更新时间:2023-12-01 05:21:48 25 4
gpt4 key购买 nike

这是我现在拥有的。

接收者:

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 之类的数字。我在另一台计算机上尝试了它,因为它说了一些关于软件的内容,而且它是完全相同的。有谁知道为什么会发生这种情况?我花了一天时间试图弄清楚,但一无所获。非常感谢任何帮助。

最佳答案

请阅读此answer ,我认为除此之外没有什么可说的,而且我在您的代码中没有看到任何会关闭连接的内容。

来自 OTN 讨论

WinSock description: The error can occur when the local network system aborts a connection. This would occur if WinSock aborts an established connection after data retransmission fails (receiver never acknowledges data sent on a datastream socket).

TCP/IP scenario: A connection will timeout if the local system doesn't receive an (ACK)nowledgement for data sent. It would also timeout if a (FIN)ish TCP packet is not ACK'd (and even if the FIN is ACK'd, it will eventually timeout if a FIN is not returned).

It seems to happen more with WindowsXP and it seems also to be possibly related to Windows firewall settings. In any case the salient point is that the abort has originated inside the local machine.

关于java - 通过流发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10301325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com