【问题标题】:How should I properly convert mp3 to byte array and then convert it back to mp3我应该如何正确地将 mp3 转换为字节数组,然后将其转换回 mp3
【发布时间】:2020-09-10 04:12:07
【问题描述】:

我正在制作一个项目,其中一部分是关于使用 TCP 将 mp3 文件从客户端传输到服务器。我的想法是客户端将通过 FileInputStream 将 mp3 转换为字节数组,连接到套接字的输出流将字节数组传递给服务器。服务器会通过socket的Input Stream获取字节数组,并通过FileOutputStream将其转换回mp3文件。

但是,我有两个问题。首先,程序运行良好,但是服务器转换的最终mp3文件是一个空文件,仅包含1个字节,而FileOutputStream接收和使用的字节数组不为空。我知道当我的服务器试图将字节数组转换回 mp3 时一定有问题,因为只使用 FileOutputStream 似乎太容易了,我可能误解了它的功能,所以我想知道如何正确地从套接字接收字节数组。

其次,我尝试比较两个程序中的字节数组,发现它们是不同的。一部分是一样的,但大部分是不同的,尤其是字节数组的开头和结尾,我不知道为什么。我对如何从套接字使用 InputStream 和 OutputStream 有概念上的问题吗?

这是发送 mp3 的客户端的部分代码:

public static void sendPackets(){
        System.out.println("Sending test file...");
        try{
            while (active){
                File file = new File("Sorrow.mp3"); // Sorrow.mp3 is the local mp3 music needs to be sent
                FileInputStream loc = new FileInputStream(file);
                sendData = new byte[(int)file.length()];
                loc.read(sendData);
                //socket_tcp is a Socket object connecting to the server
                OutputStream fis = socket_tcp.getOutputStream(); 
                fis.write(sendData);
                fis.flush();
                fis.close();
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这是服务器的代码,它接收字节数组并将其转换回mp3:

/** This is the function for the thread listening and receiving the music
  * active is a boolean value tracking whether the server is suppose to keep running or not
 **/

public static void listen() {
        while (active) {
            try {
                //Wait until packet is received
                // listenSocket is a ServerSocket specific for this thread
                socket = listenSocket.accept();
                System.out.println("We got music from the client!");
                File file = new File("Song.mp3");
                InputStream is = socket.getInputStream();
                receiveData = new byte[1024];
                is.read(receiveData);
                System.out.println(Arrays.toString(receiveData));
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(receiveData);
                fos.flush();
                fos.close();

            } catch (IOException e) {
                if(active) {
                    listen();
                } else {
                    break;
                }
            }
        }

我是这个社区的新手,所以如果我在提问时犯了任何错误,请告诉我,谢谢!任何帮助表示赞赏!

【问题讨论】:

    标签: java sockets audio tcp client-server


    【解决方案1】:

    我没有尝试调试您的确切问题,但我有一些 cmets。

    你的 receiveData 缓冲区只有 1K

    您的 receiveData 缓冲区只有 1K。我想你的 MP3 比这更长。

    receiveData = new byte[1024];
    is.read(receiveData);
    

    首先以更简单的方式尝试流

    与其通过套接字发送文件,不如尝试编写一些代码将文件复制到另一个文件夹(即从 FileInputStream 到 FileOutputStream)。一旦你让它可靠地工作,并且你掌握了流的窍门,你就可以使用相同的代码跨套接字编写。套接字增加了很多额外的复杂性——更多的移动部件——这使得调试变得更加困难。

    缓冲区不是必须的

    在将文件写入输出流之前将文件读入缓冲区是一种速度优化。您可以通过从InputStream 读取并直接逐字节写入OutputStream 来简化代码(至少从一开始)。一旦你得到了那个更简单的版本,如果你发现它表现不佳,你可以引入一个缓冲区。在实践中,使用缓冲区可能没有太大区别,因为操作系统自己会进行大量的抢先式缓存。

    read() 应该在一个循环中

    InputStream 上的 read() 方法返回读取的总字节数。不能保证一次性读取所有字节。您应该始终将read() 放在一个循环中,并使用返回的数字作为数组的偏移量以供下一次读取,直到您收到-1 来表示文件结束。

    byte[] buffer = new byte[bufferSize];
    int bytesRead = 0;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
    

    对资源使用 try

    为确保即使在引发异常时也能释放资源,您应该养成使用 try..finally 块的习惯,或者更好的是使用为您调用 close()try-with-resources 块。

    例子:

    FileOutputStream fos = new FileOutputStream(file);
    try {
        fos.write(receiveData);
    } finally {
        fos.close();
    }
    

    或者更简洁的等价于 try-with-resources 块:

    try (FileOutputStream fos = new FileOutputStream(file)) {
        fos.write(receiveData);
    }
    

    不需要flush()

    不需要flush()close()。后者在关闭流之前自动刷新。

    【讨论】:

      猜你喜欢
      • 2014-04-14
      • 2020-04-02
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2011-04-01
      • 2017-07-06
      相关资源
      最近更新 更多