2015-05-25 59 views
0

我有記錄音頻的控制器方法和javascript方法。如何在javascript中處理真正的字符串數據

控制器:

該控制器將返回字符串數據

@RequestMapping(value = "/sendAudioBlobData/{questionId}", headers = "content-type=multipart/*", method = { RequestMethod.GET, RequestMethod.POST }) 
    @ResponseBody 
    public String getBlob(HttpServletRequest request,HttpServletResponse response, ModelMap model, @RequestParam(value = "file") MultipartFile file, @PathVariable(value = "questionId") int questionId) { 

     boolean flag = false; 

     String id = Integer.toString(questionId); 

     String fileName = null; 

     byte[] bytes; 
     try { 

      bytes = file.getBytes(); 

      if (!file.isEmpty()) { 

       id = id + ".mp3"; 


       fileName = VOPortalConstants.NLE_USER_AUDIO_FILES_ROOT + File.separator + id; 

       BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(fileName))); 
       stream.write(bytes); 
       stream.close(); 

       File f = new File(fileName); 

       if(f.exists()) { 
        response.addHeader("Content-Length", f.length() + ""); 
        FileUtils.sendFile(f, response.getOutputStream()); 
       } 

       } 

     } 

     catch (IOException e) { 

      logger.error("ERROR: ", e); 
     } 

     String fName = fileName.replace(VOPortalConstants.NLE_PATH, VOPortalConstants.NLE_URL_CONST); 

     return fName; 

    } 

JS方法:

該代碼提供給請求(POST),用於記錄用戶的音頻

function saveAudioFile() { 
    var questionId = window.questionList[window.counter].id; 

     recorder && recorder.exportWAV(function(blob) { 

     var formData = new FormData(); 
     formData.append("file", blob); 
     $.ajax({ 
      url : "sendAudioBlobData/" + questionId, 
      type: 'POST', 
      data: formData, 
      dataType: "text", 
      contentType: false, 
      processData: false, 
      success: function(data) { 
       debugger 

       var audio = $("#your_voice");  
       $("#your_voice_source").attr("src", data); 
       /****************/ 
       audio[0].pause(); 
       audio[0].load();//suspends and restores all audio element 
       //audio[0].play(); 
       /****************/ 
      }, 
      error: function() { 
      alert("not so boa!"); 
      } 
     }); 
     }); 
    } 

在控制器,返回值是字符串,它看起來如下 - > FNAME =/NLERESOURCES/NLE/VOICE/USERVOICE \ 107.mp3

但返回數據看起來如下在js - > RIFF $ @WAVEfmt

我做的如何處理真正的字符串數據?

由於

+0

#your_voice是

回答

0

FileUtils.sendFile在文件返回作爲來自控制器的響應OutputStream的一部分。

相關問題