I am uploading a wav file from Chrome using Javascript as seen on Saving WAV File Recorded in Chrome to Server. I am using java for my backend and I followed this topic Sending a blob to a servlet through ajax. The file was successfully created.However it seems to be damaged and can not be read. I am wondering how to properly make a wav file.
Send the blob using ajax:
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);
The java code :
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();
}
}