将Blob转换为Wav文件

时间:2016-07-18 14:22:11

标签: javascript java ajax servlets blob

我正在使用Javascript从Chrome上传wav文件,如Saving WAV File Recorded in Chrome to Server所示。我在我的后端使用java,我遵循了这个主题Sending a blob to a servlet through ajax。该文件已成功创建。但它似乎已损坏,无法读取。我想知道如何正确制作一个wav文件。

使用ajax发送blob:

 var xhr = new XMLHttpRequest();
  xhr.onload = function (e) {
      if (this.readyState === 4) {
          console.log("Server returned: ", e.target.responseText);
      }
  };
  var fd = new FormData();
  fd.append("Wav", blob);
  xhr.open("POST", "servletUrl", true);
  xhr.send(fd);

java代码:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    try {

        byte[] buffer = new byte[1024 * 1024];
        InputStream input = request.getInputStream();       
        OutputStream output = new FileOutputStream("G:\\test.wav");
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1){
            System.out.println(bytesRead);
            output.write(buffer, 0, bytesRead);
        }
        output.close();
        input.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

0 个答案:

没有答案