有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将字节流转换为。wav文件

我试图制作一个程序,将文件从服务器发送到客户端(通过socket)。服务器转换一个文件。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) 个答案