将字节流转换为.wav文件

时间:2014-09-04 22:08:08

标签: java sockets audio bytearray wav

我正在尝试制作一个程序,将文件从服务器发送到客户端(通过套接字)。服务器将.wav文件转换为字节数组并将其发送到客户端。我的问题是我无法播放.wav文件(在客户端)因为它已损坏。我做了一些研究,发现为此你不能只使用FileOutputStream或BufferedOutputStream。我只是不知道如何将字节数组转换为.wav文件,以便它可以播放。也许有人知道我的答案?

服务器代码:

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

@SuppressWarnings({ "resource", "unused" })
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();
}
}
}

感谢您的帮助!

编辑:我在线比较了两个音乐文件,我发现Java在文件开头添加了大量的空格。这就是为什么它不会运行。任何人都知道如何解决这个问题?

0 个答案:

没有答案