2014-08-29 60 views
1

我真的被一個小小的項目所困擾。我試圖通過套接字將服務器上的音樂文件(僅.wav)發送到客戶端。一切工作完全正常(我認爲...),除了客戶端收到的文件不完整。我無法播放該文件,並且我可以看到它比服務器的尺寸稍小。我在做什麼不對?Java - 通過套接字發送文件 - 文件未完全收到

這裏是服務器代碼:

private Socket client; 
private String filename; 
private TBMCAudioServer ac; 

private FileInputStream fis; 
private BufferedOutputStream out; 

int bufferSize = 0; 

FileSender(Socket client, String filename, TBMCAudioServer ac){ 

    this.client = client; 
    this.filename = filename; 
    this.ac = ac; 
} 

@Override 
public void run(){ 

    ac.ex.sendMessage(client, "[#preload#]" + filename); 

    File dir = new File(ac.getDataFolder() + File.separator + "music"); 
    if(!dir.exists()){ 
     dir.mkdir(); 
    } 

    File file = new File(dir, filename + ".wav"); 

    long length = file.length(); 
    if(length > Integer.MAX_VALUE){ 
     logger.info("File is too large."); 
    } 
    byte[] bytes = new byte[(int) length]; 

    try{ 
     fis = new FileInputStream(file); 
     out = new BufferedOutputStream(client.getOutputStream()); 
    } catch (IOException e){ 
     logger.info(e.getMessage()); 
    } 

    int count; 

    try { 
     while((count = fis.read(bytes,0,bytes.length)) != -1){ 
      out.write(bytes, 0, count); 
     } 

     out.flush(); 
     out.close(); 
     fis.close(); 
    } catch (IOException e) { 
     logger.info(e.getMessage()); 
    } 

} 

,在這裏你可以看到我的客戶端代碼:

private Socket server; 
private String filename; 
private AudioClient ac; 

InputStream is = null; 
FileOutputStream fos = null; 
int bufferSize = 0; 

FileReceiver(Socket server, String filename, AudioClient ac){ 
    this.server = server; 
    this.filename = filename; 
    this.ac = ac; 
} 

@Override 
public void run() { 

    try{ 
     is = server.getInputStream(); 

     bufferSize = server.getReceiveBufferSize(); 
     ac.logConsole("Buffer size: " + bufferSize); 
    } catch (IOException ex){ 
     ac.logConsole(ex.getMessage()); 
    } 

    try{ 
     fos = new FileOutputStream(AudioClient.util.getLineValue(3) + filename + ".wav"); 
    } catch (FileNotFoundException e){ 
     ac.logConsole(e.getMessage()); 
    } 

    byte[] bytes = new byte[bufferSize]; 

    int count; 

    try { 
     while((count = is.read(bytes, 0, bytes.length)) != -1){ 
      fos.write(bytes, 0, count); 
     } 

     ac.logConsole("yay"); 
     is.close(); 
     fos.flush(); 
     fos.close(); 
    } catch (IOException e) { 
     ac.logConsole(e.getMessage()); 
    } 
} 
+0

沒有沒有,我會嘗試,現在 – 2014-08-29 09:13:08

+0

有人胡說,我刪除了評論。 – Fildor 2014-08-29 09:13:49

+0

我試過了,確實沒有用。仍然感謝您試圖幫助! – 2014-08-29 09:15:34

回答

1

好吧,我設法發送的文件充分,所以我可以看到它具有相同的大小因爲它來自我的新代碼。唯一的問題是我發送音樂文件,我無法播放發送的文件。也許有人知道問題是什麼?

Server代碼:

package me.Ciaran.simplefileserver; 

import java.io.BufferedInputStream; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class SimpleFileServer { 

public final static int SOCKET_PORT = 13267; // you may change this 
public final static String FILE_TO_SEND = "D:/server/plugins/TBMCAudioServer/music/DLTALL.wav"; // you may change this 

public static void main (String [] args) throws IOException { 
FileInputStream fis = null; 
BufferedInputStream bis = null; 
OutputStream os = null; 
ServerSocket servsock = null; 
Socket sock = null; 
try { 
    servsock = new ServerSocket(SOCKET_PORT); 
    while (true) { 
    System.out.println("Waiting..."); 
    try { 
     sock = servsock.accept(); 
     System.out.println("Accepted connection : " + sock); 
     // send file 

     final File myFile= new File(FILE_TO_SEND); //sdcard/DCIM.JPG 
     byte[] mybytearray = new byte[8192]; 
     fis = new FileInputStream(myFile); 
     bis = new BufferedInputStream(fis); 
     DataInputStream dis = new DataInputStream(bis); 
     try { 
      os = sock.getOutputStream(); 
      DataOutputStream dos = new DataOutputStream(os); 
      dos.writeUTF(myFile.getName());  
      dos.writeLong(mybytearray.length); 
      int read; 
      while((read = dis.read(mybytearray)) != -1){ 
       dos.write(mybytearray, 0, read); 
      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     System.out.println("Done."); 
    } 
    finally { 
     if (bis != null) bis.close(); 
     if (os != null) os.close(); 
     if (sock!=null) sock.close(); 
    } 
    } 
} 
finally { 
    if (servsock != null) servsock.close(); 
} 
} 
} 

這裏是客戶端代碼:

package me.Ciaran.simplefileclient; 

import java.io.BufferedOutputStream; 
import java.io.DataInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.Socket; 

public class SimpleFileClient { 

public final static int SOCKET_PORT = 13267;  // you may change this 
public final static String SERVER = "127.0.0.1"; // localhost 
public final static String 
    FILE_TO_RECEIVED = "C:/Users/Ciaran/Documents/TESTEN/music/DLTALL.wav"; // you may change this, I give a 
                 // different name because i don't want to 
                 // overwrite the one used by server... 

    public final static int FILE_SIZE = 6022386; // file size temporary hard coded 
              // should bigger than the file to be downloaded 

public static void main (String [] args) throws IOException { 
int bytesRead; 
int current = 0; 
FileOutputStream fos = null; 
BufferedOutputStream bos = null; 
Socket sock = null; 
try { 
    sock = new Socket(SERVER, SOCKET_PORT); 
    System.out.println("Connecting..."); 

    // receive file 

    InputStream in; 
    int bufferSize=0; 

    try { 
     bufferSize=sock.getReceiveBufferSize(); 
     in=sock.getInputStream(); 
     DataInputStream clientData = new DataInputStream(in); 
     String fileName = clientData.readUTF(); 
     System.out.println(fileName); 
     OutputStream output = new FileOutputStream(FILE_TO_RECEIVED); 
     byte[] buffer = new byte[bufferSize]; 
     int read; 
     while((read = clientData.read(buffer)) != -1){ 
      output.write(buffer, 0, read); 
     } 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    System.out.println("File " + FILE_TO_RECEIVED 
     + " downloaded (" + current + " bytes read)"); 
} 
finally { 
    if (fos != null) fos.close(); 
    if (bos != null) bos.close(); 
    if (sock != null) sock.close(); 
} 
} 

}