2016-05-08 58 views
0

我可以傳輸文件,但是當我想打開它時說明文件已損壞(因爲它的0字節長)。 TTCPServer/TCP客戶端文件傳輸問題:0字節

當我啓動TCPServer時,它會等待客戶端並接受它們,然後將文件發送給它們。客戶端收到文件(但不是我想象中的所有文件)當我用一個10kb的picture.png嘗試這個文件時,它工作正常。與其他任何東西都沒有。我還做了端口轉發(否則客戶不可能得到文件)

這是TCPSERVER:

import java.io.*; 
import java.net.*; 

class TCPServer { 

    private final static String fileToSend = "C:/Users/Tim/Desktop/P&P/Background music for P&P/Rock.wav"; 

    public static void main(String args[]) { 

     while (true) { 
      ServerSocket welcomeSocket = null; 
      Socket connectionSocket = null; 
      BufferedOutputStream outToClient = null; 

      try { 
       welcomeSocket = new ServerSocket(3222); 
       connectionSocket = welcomeSocket.accept(); 
       outToClient = new BufferedOutputStream(
         connectionSocket.getOutputStream()); 
      } catch (IOException ex) { 
       // Do exception handling 
      } 

      if (outToClient != null) { 
       File myFile = new File(fileToSend); 
       byte[] mybytearray = new byte[(int) myFile.length()]; 

       FileInputStream fis = null; 

       try { 
        fis = new FileInputStream(myFile); 
       } catch (FileNotFoundException ex) { 
        // Do exception handling 
       } 
       BufferedInputStream bis = new BufferedInputStream(fis); 

       try { 
        bis.read(mybytearray, 0, mybytearray.length); 
        outToClient.write(mybytearray, 0, mybytearray.length); 
        outToClient.flush(); 
        outToClient.close(); 
        connectionSocket.close(); 

        // File sent, exit the main method 
        return; 
       } catch (IOException ex) { 
        // Do exception handling 
       } 
      } 
     } 
    } 
} 





HERE IS THE TCP CLIENT: 

import java.io.*; 
import java.net.*; 

import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.SourceDataLine; 

class TCPClient { 

    private final static String serverIP = "123.123.123.123"; 
    private final static int serverPort = 3222; 
    private final static String fileOutput = "C:/Users/Daniel/Desktop/check.wav"; 

    public static void main(String args[]) { 

     while (true) { 

      byte[] aByte = new byte[1024]; 
      int bytesRead; 

      Socket clientSocket = null; 
      InputStream is = null; 

      try { 
       clientSocket = new Socket(serverIP, serverPort); 
       is = clientSocket.getInputStream(); 
      } catch (IOException ex) { 
       // Do exception handling 
      } 

      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

      if (is != null) { 

       FileOutputStream fos = null; 
       BufferedOutputStream bos = null; 
       try { 
        fos = new FileOutputStream(fileOutput); 
        bos = new BufferedOutputStream(fos); 
        bytesRead = is.read(aByte, 0, aByte.length); 

        do { 
         baos.write(aByte); 
         bytesRead = is.read(aByte); 
        } while (bytesRead != -1); 

        bos.write(baos.toByteArray()); 
        bos.flush(); 
        bos.close(); 
        clientSocket.close(); 
       } catch (IOException ex) { 
        // Do exception handling 
       } 
      } 

      // Music is played here 
      try { 

       AudioInputStream input = AudioSystem 
         .getAudioInputStream(new File(fileOutput)); 
       SourceDataLine line = AudioSystem.getSourceDataLine(input 
         .getFormat()); 
       line.open(input.getFormat()); 
       line.start(); 
       byte[] buffer = new byte[1024]; 
       int count; 
       while ((count = input.read(buffer, 0, 1024)) != -1) { 
        line.write(buffer, 0, count); 
       } 
       line.drain(); 
       line.stop(); 
       line.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

回答

0

重構的客戶端代碼一點點:

  • 沒有必要的ByteArrayOutputStream時已經在使用的BufferedOutputStream
  • 使用bytesRead爲字節數組偏移

這個工作對我來說:

if (is != null) 
     { 

      FileOutputStream fos = null; 
      BufferedOutputStream bos = null; 
      try 
      { 
       fos = new FileOutputStream(new File(fileOutput)); 
       bos = new BufferedOutputStream(fos); 

       while ((bytesRead = is.read(aByte)) != -1) 
       { 
        bos.write(aByte, 0, bytesRead); 
       } 
       bos.flush(); 
       bos.close(); 
       clientSocket.close(); 
      } catch (IOException ex) 
      { 
       ex.printStackTrace(); 
      } 
     } 
+0

現在這happend:java.net.SocketException異常:連接復位 \t在java.net.SocketInputStream.read(來源不明) \t在java.net.SocketInputStream.read(未知源) \t在java.net.SocketInputStream.read(來源不明) \t在TCPClient.main(TCPClient.java:57) java.io.EOFException的 \t在java.io.DataInputStream.readInt(來源不明) \t at com.sun.media.sound.WaveFileReader.getFMT(Un已知源) \t在com.sun.media.sound.WaveFileReader.getAudioInputStream(未知來源) \t在javax.sound.sampled.AudioSystem.getAudioInputStream(未知來源) \t在TCPClient.main(TCPClient.java:74) 。 – Tibi

相關問題