我可以使用下面的代码将任何类型的文件上传到服务器。但是,在上传完成后,我在服务器上收到一条错误消息
java.net.SocketException: Socket is closed
Exception in thread "Thread-0" java.lang.NullPointerException
at Server2Connection.run(server1.java:407)
at java.lang.Thread.run(Unknown Source)
server1.java: 407
指的是服务器代码中的行switch (clientMsg)
。该文件确实上传正确。但似乎我没有用.close
指令做正确的事情。此后服务器不会断开连接,但客户端只会再次循环断开连接。任何人都可以告诉我在哪里弄乱吗?感谢。
服务器端:
public BufferedReader msgFromClient() throws IOException {
BufferedReader receiveClientmsg = null;
try {
receiveClientmsg = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
System.out.println(e);
}
return receiveClientmsg;
}
public DataOutputStream outToClient() throws IOException {
DataOutputStream sendClientmsg = null;
try {
sendClientmsg = new DataOutputStream(clientSocket.getOutputStream());
} catch (IOException e) {
System.out.println(e);
}
return sendClientmsg;
}
public void upload() throws IOException {
byte[] mybytearray = new byte[8192];
InputStream is = clientSocket.getInputStream();
String file_name = msgFromClient().readLine();
File rqstd_upld_file = new File(SERVER_FILE_LOCATION + file_name);
if (rqstd_upld_file.exists()){
outToClient().writeBytes("yes\n");
outToClient().writeBytes("A file with the name " + file_name + " already exists on server\n");
System.out.println("A file with the name " + file_name + " already exists");
}
else {
outToClient().writeBytes("no\n");
FileOutputStream fos = new FileOutputStream(SERVER_FILE_LOCATION +"\\"+ file_name);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int count;
System.out.println("Downloading " + file_name + " ...");
outToClient().writeBytes("Uploading. Please wait...\n");
while ((count = is.read(mybytearray)) > 0){
bos.write(mybytearray, 0, count);
}
System.out.println("Download Successful");
is.close();
bos.close();
}
}
try {
while (true) {
//Message sent by the client
String clientMsg = server.msgFromClient().readLine();
switch (clientMsg) {
case "1":
//'Upload file' command from client
System.out.print("\nCommand from " + clientSocket.getInetAddress() + ":"+ clientSocket.getPort()+" : ");
System.out.println("Upload file");
server.upload();
break;
case "1fail":
//In case client fails to find the file it wants to upload
System.out.print("\nCommand from " + clientSocket.getInetAddress() + ":"+ clientSocket.getPort()+" : ");
System.out.println("Upload file");
System.out.println("Client could not upload: File did not exist on client machine\n");
break;
}
catch(IOException e){
System.out.println(e);
}
客户方:
while (true) {
try{
Scanner scan0 = new Scanner(System.in);
String command = scan0.nextLine();
switch (command) {
case "1":
//Upload file
File file_to_upload = null;
System.out.print("Enter file path: ");
Scanner scan = new Scanner(System.in);
String pathname = scan.nextLine();
System.out.print("Enter file name: ");
Scanner scan1 = new Scanner(System.in);
String file = scan1.nextLine();
Path path = Paths.get(pathname, file);
file_to_upload = new File(path.toString());
if (file_to_upload.exists()){
outToServer.writeBytes(command + '\n');
outToServer.writeBytes(file + '\n');
String existsOnServer = msgFromServer.readLine();
switch (existsOnServer) {
case "yes":
System.out.println('\n'+ msgFromServer.readLine() + '\n');
break;
case "no":
int count;
System.out.println('\n'+ msgFromServer.readLine() + '\n');
byte[] bytearray = new byte[8192];
InputStream in = new FileInputStream(file_to_upload);
BufferedInputStream bis = new BufferedInputStream(in);
OutputStream os = client1.getOutputStream();
while ((count = bis.read(bytearray)) > 0){
os.write(bytearray, 0, count);
}
System.out.println("Done Uploading");
os.close();
in.close();
bis.close();
break;
}
}
else{
System.out.println("File " + path + " does not exist\n");
outToServer.writeBytes("1fail" + '\n');
}
break;
case "2":
//...
}
catch(IOException e){
System.out.println(e);
break;
}
}
答案 0 :(得分:0)
只需添加一个新命令即可关闭与服务器的连接。
switch (clientMsg) {
case "1":
//'Upload file' command from client
System.out.print("\nCommand from " + clientSocket.getInetAddress() + ":"+ clientSocket.getPort()+" : ");
System.out.println("Upload file");
server.upload();
break;
case "1fail":
//In case client fails to find the file it wants to upload
System.out.print("\nCommand from " + clientSocket.getInetAddress() + ":"+ clientSocket.getPort()+" : ");
System.out.println("Upload file");
System.out.println("Client could not upload: File did not exist on client machine\n");
break;
case "-1":
is.close();
break;
答案 1 :(得分:0)
您正在为每笔交易创建新流并关闭它们。这会关闭套接字,导致下次发生的异常。
但该技术无论如何都无效。您应该在套接字的生命周期中使用相同的流。您还需要安排对等方知道您正在发送的文件中有多少数据,通常是事先通过发送它并让对等体读取多少字节,而不是像现在一样读取到流的末尾。
答案 2 :(得分:-1)
您可能过早关闭客户端strem,不允许服务器完成读取。在关闭客户端的straem和socket之前尝试放一个计时器。如果它有效,你应该考虑一种更好的同步方式,以允许服务器读取整个文件。