我的流有问题。我正在使用多线程客户端和服务器,以便我可以一个接一个地上传或下载文件,但在获得第一个文件后(如果我向服务器发送文件或从服务器接收文件无关紧要),我无法继续使用第二个文件。
流关闭,我不知道如何在不损坏我的文件的情况下维护流。没有"流"。Close和其他一些方法
我知道这不是最好的主意,可能完全不能接受,只是我现在完全迷路了。我哪里搞砸了?
服务器部分:public class ServerCommunicationThread extends Thread
{
...
public ServerCommunicationThread(Socket socket) throws IOException,
ParserConfigurationException, TransformerException
{
...
}
public synchronized void run()
{
try
{
String clientSelection;
while ((clientSelection = inFromClient.readLine()) != null)
{
switch (clientSelection)
{
case "1":
receiveFile();
break;
case "2":
String outGoingFileName;
while ((outGoingFileName = inFromClient.readLine()) != null)
{
sendFile(outGoingFileName);
}
break;
case "3":
{
disconnect();
break;
}
default:
System.out.println("Incorrect command received.");
break;
}
}
}
catch (IOException e)
{
// System.out.println(clientSocket + "---> Action performed");
e.printStackTrace();
}
}
public synchronized void receiveFile()
{
try
{
int bytesRead;
DataInputStream clientData = new DataInputStream(
clientSocket.getInputStream());
String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream((filePath
+ "received_from_client_" + fileName));
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0
&& (bytesRead = clientData.read(buffer, 0,
(int) Math.min(buffer.length, size))) != -1)
{
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
clientData.close();
System.out.println("File " + fileName + " received from client.");
}
catch (IOException ex)
{
System.err.println("Client error. Connection closed.");
}
}
public synchronized void sendFile(String fileName)
{
try
{
File myFile = new File(fileName);
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fileInStream = new FileInputStream(myFile);
BufferedInputStream bufferedInStream = new BufferedInputStream(
fileInStream);
DataInputStream dataInStream = new DataInputStream(bufferedInStream);
dataInStream.readFully(mybytearray, 0, mybytearray.length);
OutputStream outStream = clientSocket.getOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(outStream);
dataOutStream.writeUTF(myFile.getName());
dataOutStream.writeLong(mybytearray.length);
dataOutStream.write(mybytearray, 0, mybytearray.length);
dataOutStream.flush();
System.out.println("File " + fileName + " sent to client.");
}
catch (Exception e)
{
System.err.println("File does not exist!");
}
}
}
客户:public class ClientConnectAndSender extends Thread
{
...
public ClientConnectAndSender() throws IOException
{
...
}
public synchronized void startSending() throws IOException,
ParserConfigurationException, TransformerException
{
while (ServerRunning)
{
outStream = new PrintStream(clientSocket.getOutputStream());
try
{
switch (Integer.parseInt(selectAction()))
{
case 1:
outStream.println("1");
sendFile();
break;
case 2:
outStream.println("2");
System.err.print("Enter file name: ");
fileName = inFromServer.readLine();
outStream.println(fileName);
receiveFile(fileName);
break;
case 3:
outStream.println("3");
disconnect();
break;
}
}
catch (Exception e)
{
System.err.println("not valid input");
}
recieverThread = new ClientRecieverThread(inFromServer);
recieverThread.start();
}
}
public String selectAction() throws IOException
{
System.out.println("1. Send file.");
System.out.println("2. Recieve file.");
System.out.println("3. End session");
System.out.print("nMake selection: ");
return inFromServer.readLine();
}
public synchronized void sendFile()
{
try
{
System.err.print("Enter file name: ");
fileName = inFromServer.readLine();
File myFile = new File(fileName);
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fileInStream = new FileInputStream(myFile);
BufferedInputStream bufferedInStream = new BufferedInputStream(
fileInStream);
// bis.read(mybytearray, 0, mybytearray.length);
DataInputStream dataInStream = new DataInputStream(bufferedInStream);
dataInStream.readFully(mybytearray, 0, mybytearray.length);
OutputStream outStream = clientSocket.getOutputStream();
// Sending file name and file size to the server
DataOutputStream dataOutStream = new DataOutputStream(outStream);
dataOutStream.writeUTF(myFile.getName());
dataOutStream.writeLong(mybytearray.length);
dataOutStream.write(mybytearray, 0, mybytearray.length);
dataOutStream.flush();
System.out.println("File " + fileName + " sent to Server.");
}
catch (Exception e)
{
System.err.println("File does not exist!");
}
}
public synchronized void receiveFile(String fileName)
{
try
{
int bytesRead;
InputStream inputStream = clientSocket.getInputStream();
DataInputStream clientData = new DataInputStream(inputStream);
fileName = clientData.readUTF();
OutputStream outputStream = new FileOutputStream((filePath
+ "received_from_server_" + fileName));
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0
&& (bytesRead = clientData.read(buffer, 0,
(int) Math.min(buffer.length, size))) != -1)
{
outputStream.write(buffer, 0, bytesRead);
size -= bytesRead;
}
// outputStream.flush();
outputStream.close();
inputStream.close();
// try
// {
// in.available();
// output.flush();
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
System.out.println("File " + fileName + " received from Server.");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
服务器端的clientData.close()
和客户端的inputStream.close();
正在关闭套接字....