Java 为什么fileOutputStream可以';在客户机-服务器应用程序中的空行(或换行符)后,是否不输出任何字符串?
在我的客户机-服务器应用程序中,当我尝试从服务器端下载一个文件时,该文件会被下载;但是标题信息,例如一些带有换行符的附加文本,无法读取。正如您在我的服务器类中看到的,以下信息应该在客户端打印出来Java 为什么fileOutputStream可以';在客户机-服务器应用程序中的空行(或换行符)后,是否不输出任何字符串?,java,file-io,client-server,fileinputstream,fileoutputstream,Java,File Io,Client Server,Fileinputstream,Fileoutputstream,在我的客户机-服务器应用程序中,当我尝试从服务器端下载一个文件时,该文件会被下载;但是标题信息,例如一些带有换行符的附加文本,无法读取。正如您在我的服务器类中看到的,以下信息应该在客户端打印出来 outputToClient.printf("Status OK\r\nSize %d Bytes\r\n\r\nFile %s Download was successfully\r\n", fileSize, filename)
outputToClient.printf("Status OK\r\nSize %d Bytes\r\n\r\nFile %s Download was successfully\r\n",
fileSize, filename);
但事实并非如此。仅打印\r\n
之前的第一个字符串(状态正常
),而不是\r\n
之后的任何字符串
我怎样才能修好它
我的完整代码:
服务器端:
public class ServerSide {
private BufferedReader inputFromClient;
private PrintWriter outputToClient;
private FileInputStream fis;
private OutputStream os;
private static final int PORT = 8000;
private ServerSocket serverSocket;
private Socket socket;
public static void main(String[] args) {
int port = PORT;
if (args.length == 1) {
port = Integer.parseInt(args[0]);
}
new ServerSide(port);
}
private boolean fileExists(File[] files, String filename) {
boolean exists = false;
for (File file : files) {
if (filename.equals(file.getName())) {
exists = true;
}
}
return exists;
}
public ServerSide(int port) {
// create a server socket
try {
serverSocket = new ServerSocket(port);
} catch (IOException ex) {
System.out.println("Error in server socket creation.");
System.exit(1);
}
while (true) {
try {
socket = serverSocket.accept();
outputToClient = new PrintWriter(socket.getOutputStream());
inputFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String request = inputFromClient.readLine();
if (!request.startsWith("exit") && !request.startsWith("pwd") && !request.startsWith("list") && !request.startsWith("GET")) {
outputToClient.println("Wrong request\r\n"
+ "\r\n");
} else if (request.startsWith("exit")) {
break;
} else if (request.startsWith("pwd")) {
File file = new File(System.getProperty("user.dir"));
outputToClient.print("Status OK\r\n"
+ "Lines 1\r\n"
+ "\r\n"
+ "Working dir: " + file.getName() + "\r\n");
} else if (request.startsWith("list")) {
File file = new File(System.getProperty("user.dir"));
File[] files = file.listFiles();
outputToClient.print("Status OK\r\n"
+ "Files " + files.length + "\r\n"
+ "\r\n"
+ Arrays.toString(files).substring(1, Arrays.toString(files).length() - 1) + "\r\n");
} else if (request.startsWith("GET")) {
String filename = request.substring(4);
File file = new File(System.getProperty("user.dir"));
File[] files = file.listFiles();
if (fileExists(files, filename)) {
file = new File(filename);
int fileSize = (int) file.length();
outputToClient.printf("Status OK\r\nSize %d Bytes\r\n\r\nFile %s Download was successfully\r\n",
fileSize, filename);
outputToClient.flush();
try (FileInputStream fis = new FileInputStream(file)) {
os = socket.getOutputStream();
byte[] buffer = new byte[(1 << 7) - 1];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
//os.close();
// fis.close();
} else {
outputToClient.print("Status 400\r\n"
+ "File " + filename + " not found\r\n"
+ "\r\n");
outputToClient.flush();
}
}
outputToClient.flush();
}
} catch (IOException e) {
System.err.println(e);
}
}
}
}
}
通常误用ready()。
尝试以下方法:
while ((line = inputFromServer.readLine()) != null)
System.out.println(line);
if (line.isEmpty()) {
Connected = false;
break;
}
// ...
注意:您根本不需要
fileExists()
方法,当然也不需要这样编写<如果文件不存在,code>newfileinputstream()将抛出FileNotFoundException
。捕捉它。您应该在上次outputToClient.flush()之后关闭服务器端中的输出流outputToClient.close()
在客户端,您需要再次建立连接,因此将连接部分移动到循环中。为什么不能执行string.replaceAll(System.getProperty(“line.separator”),“”);或者逐行阅读??我试过了,但没用。它甚至不允许用户输入GET命令。outputToClient.close()导致“java.net.SocketException:软件导致的连接中止:recv失败该错误”更新了答案,因为流已关闭,您需要重新建立连接
while (Connected) {
line = inputFromServer.readLine();
System.out.println(line);
if (line.isEmpty()) {
Connected = false;
if (inputFromServer.ready()) {
System.out.println(inputFromServer.readLine());
}
}
// ...
while ((line = inputFromServer.readLine()) != null)
System.out.println(line);
if (line.isEmpty()) {
Connected = false;
break;
}
// ...