根据大家的建议编辑我的代码,并将代码压缩到可以查明导致问题的代码行的位置。 服务器代码:`
公共(public)类 Server2 {
public static void main(String args[]) throws Exception {
ServerSocket servsock = null;
Socket sock = null;
int SOCKET_PORT = 12362;
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_SEND = "SFileToBeSent.txt";
String FILE_TO_RECEIVED = "CFileReceived.txt";
int FILE_SIZE = 6022386;
try {
System.out.println("Server created.");
servsock = new ServerSocket(SOCKET_PORT); // creates servsock with socket port
while (true) {
System.out.println("Waiting for connection...");
try {
sock = servsock.accept(); // accepts a socket connection
System.out.println("Accepted connection : " + sock);
System.out.println();
os = sock.getOutputStream(); // sets Output Stream using the sockets output stream
is = sock.getInputStream(); // get input stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
// if any streams or sockets are not null, the close them
if (os != null)
os.close();
if (is != null)
is.close();
if (sock != null)
sock.close();
} // end of finally
} // end of while
} // end of try
finally {
if (servsock != null)
servsock.close();
} // end of finally
}
}
公共(public)类 Client2 {
public static void main(String args[]) throws Exception {
Socket sock = null; // used in main
String SERVER = "localhost"; // local host
int SOCKET_PORT = 12362; // you may change this
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_RECEIVED = "SFileReceived.txt";
String FILE_TO_SEND = "CFileToBeSent.txt";
int FILE_SIZE = 6022386;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");
System.out.println();
// get input and output from socket
is = sock.getInputStream(); // get input stream from socket
os = sock.getOutputStream(); // get output stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
if (sock != null)
sock.close();
if (os != null)
os.close();
if (is != null)
is.close();
} // end of finally
}
}
服务器输出:--------------------
服务器已创建。
正在等待连接...
已接受连接:Socket[addr=/127.0.0.1,port=51565,localport=12362]
发送SFileToBeSent.txt(32字节)
要发送的文件中的字节数组:[B@4e25154f
发送文件完成
接收到的文件可用字节数:0
要接收的文件中的字节数组:[B@70dea4e
客户端输出--------------------------------
正在连接...
接收到的文件可用字节数:32
要接收的文件中的字节数组:[B@4e25154f
读取的字节数:32
bos.write
当前 += 读取的字节数
<小时/>我仍然遇到 InputStream 的问题,并且通过调试发现服务器和客户端都卡在接收部分中的 while() 语句上。对于服务器来说,它会立即停止,而客户端会执行一次 while 循环,然后在遇到 while 语句时停止。
如果有人有任何建议或解决方案,我们将不胜感激!
最佳答案
首先,您的 receiveFile 方法应该循环读取输入并写入输出。 其次,完成后请关闭文件,否则可能会出现资源泄漏。
public static void receiveFile() 抛出 IOException {
byte[] mybytearray = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
while ((bytesRead = is.read(mybytearray)) != -1) {
bos.write(mybytearray, 0, bytesRead );
current += bytesRead;
}
bos.flush(); // clears the buffer
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
bos.close();
}
}
关于java - 通过套接字双向文件传输中的输入流干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61169641/