title: Obtaining the wav file from ServletInputStream
body: <p>Our current project requires us to send an audio file to the server and then use the audio file for further computation.</p>
  
  <p>Using the Java sound api, I was able to capture the recording and save it as a wav file in my system. Then in order to pass the audio wav to the server, I am using Apache Commons HttpClient to post a request to the server. (I am using InputstreamEntity provided by apache and sending the data as a chunk).</p>
  
  <p>The problem appears when i am trying to recreate/retrieve the wav file on the server. I understand that I would have to use the AudioSystem.write API to create the wav file (exactly as what was done on my system). However what I observe is that althought the file gets created , it does not play (I am using vlc media player to test it FYI). I have searched in Google for sample codes and have tried to implement it, but is unable to play it once the file gets created. </p>
  
  <p>The sample code snippets indicates the approaches i have tried:</p>
  
  <pre><code>//******************************************************************
  
  try {
  InputStream is = request.getInputStream();
  FileOutputStream fs = new FileOutputStream("output123.wav");
  byte[] tempbuffer = new byte[4096];
  int bytesRead;
  while((bytesRead=is.read(tempbuffer))!=-1)
  {
  fs.write(tempbuffer, 0,bytesRead);
  }
  is.close();
  fs.close();
  AudioInputStream inputStream =AudioSystem.getAudioInputStream(newFile("output123.wav"));
  int numofbytes = inputStream.available();           
  byte[] buffer = new byte[numofbytes];
  inputStream.read(buffer);
  int bytesWritten = AudioSystem.write(inputStream, AudioFileFormat.Type.WAVE,new File("outputtest.wav"));
  System.out.println("written"+bytesWritten);
  </code></pre>
  
  <p>Approach 2</p>
  
  <pre><code>InputStream is = request.getInputStream(); 
  System.out.println("inputStream obtained : "+is.toString());
  ByteArrayInputStream bais = null;
  byte[] audioBuffer = IOUtils.toByteArray(is);
  System.out.println(" is audioBuffer empty? : length =  ? "+audioBuffer.length);
  try {
  AudioFileFormat ai = AudioSystem.getAudioFileFormat(is);
  System.out.println("ai bytelength ? "+ai.getByteLength());
  System.out.println("ai frame length = "+ai.getFrameLength());
  Set&lt;Map.Entry&lt;String,Object&gt;&gt; audioProperties = ai.getFormat().properties().entrySet();
  System.out.println("entry set is empty ? "+audioProperties.isEmpty());          
  for(Map.Entry me : audioProperties){
  System.out.println("key = "+me.getKey());
  System.out.println("value ="+me.getValue());}       
  bais = new ByteArrayInputStream(audioBuffer);
  AudioInputStream ais = new AudioInputStream(bais, new AudioFormat(8000,8,2,true,true), 2);
  AudioSystem.write(ais, AudioFileFormat.Type.WAVE,new File("testtest.wav"));
  
  //*************************************************************************************
  </code></pre>
  
  <p>The <code>audioFormat</code> properties all turned out to be null. Are these null values giving the problem? So while creating the wave file on the server, I tried to set the properties manually once again. But even then the wav file would not play.</p>
  
  <p>I have also tried quite a few approaches already mentioned on this site, but somehow they aren't working. I am sure i am missing something, but I am unable to pinpoint the exact problem. </p>
  
  <p>Would be really helpful, if you guys can point out how to go about the conversion from ServletInputStream to getting a wav.</p>
  
  <p>P.S (1) I know the code is shabby, because i have been under a trial and error situation for quite some time now. But I will give more details on the approaches if needed.
   2) Apologise for the clumsiness, this happens to be my first post.. )</p>