2013-05-27 119 views
0

我對我的流有麻煩。我使用多線程客戶端和服務器,以便我可以一個接一個地上傳或下載文件,但是在獲得第一個文件後(無論我將文件發送到服務器還是從中接收文件都無關緊要),我無法繼續使用我的第二個文件。java中的流管理 - 客戶端/服務器發送和接收文件

流關閉,我無法弄清楚如何維護流而不會破壞我的文件。沒有'stream'.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(); 
     } 
    } 

} 
+0

的http:// SSCCE .org/ – AlexR

+0

@AlexR考慮到了,謝謝。 – organyx

回答

0

客戶端上的服務器和inputStream.close();上線clientData.close()被關閉套接字....

+0

非常感謝 – organyx

相關問題