Server.java
public class Server {
public static BufferedReader inFromClient = null;
public static DataOutputStream outToClient = null;
public static Socket connectionSocket = null;
static ServerSocket welcomeSocket = null;
static String path = null;
public static void main(String[] args) throws IOException {
welcomeSocket = new ServerSocket(1001);
FileInputStream fin = null;
FileOutputStream fout = null;
while (true) {
connectionSocket = welcomeSocket.accept();
inFromClient = new BufferedReader(new InputStreamReader(
connectionSocket.getInputStream()));
outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
path = inFromClient.readLine();
if (path != null) {
String fileName = new File(path).getName();
File file = new File(path);
File file2 = new File(fileName);
fin = new FileInputStream(file);
fout = new FileOutputStream(file2);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fin.read(buffer)) > 0) {
fout.write(buffer, 0, bytesRead);
}
fin.close();
fout.close();
}
}
}
}
我收到此错误
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Server.main(Server.java:37)
这就是我想要做的。
在我运行 Server.java 之后再运行 Client.java。我尝试按选择按钮,但没有选择任何文件,然后关闭文件选择器。
在我的理解是这条线
path = inFromClient.readLine();
当它尝试读取线时,但没有找到任何东西,这就是为什么它说未知来源,但我该如何解决这个问题?
在我的Client.java
我正在尝试close()
使用套接字,但它根本不关闭....
更新我已经通过在发生错误的 server.java 上放置 try&catch 来修复错误,这解决了问题!