2016-02-05 70 views
0

我們正試圖使用​​從UI端發送我們的音頻文件,下面的代碼發送音頻文件/ blob從UI到Servlet以保存在服務器上。

var url = (window.URL || window.webkitURL).createObjectURL(blob); 
var link = document.getElementById("save"); 
link.href = url; 
link.download = filename || 'output.wav'; 

var fd = new FormData(); 
    fd.append('fname', 'sample1.wav'); 
    fd.append('data', blob); 
    $.ajax({ 
     type: 'POST', 
     url: 'AudioToText', 
     data: fd, 
     processData: false, 
     contentType: "audio/wav" 
    }); 

但是,我們無法處理,在servlet的。任何人都可以用Javascript代碼幫助我將音頻文件發送到servlet和servlet代碼,以將該音頻保存在servlet中。提前致謝。

回答

0

好日子

您可以使用插件文件上傳

https://github.com/blueimp/jQuery-File-Upload

有相當完整的指令如何與春天和Ajax使用它:

http://krams915.blogspot.ru/2012/06/file-upload-with-spring-and-jquery-part_2793.html(從這個插件的維基)

快速教程(不要忘記包含插件)
HTML代碼:

<label>Name</label> 
    <form name="fileupload" method="POST" class="newSongUpload" action="upload.new.song" id="uploadNewFile"> 
    <!--in action - your url --!> 
        <input type="text" name="songName"> 
         <i class="glyphicon glyphicon-plus"></i> 
          <input type="file" name="files" id="upload-new-document" accept="your accept here"> 


     </form> 
</div> 

JS代碼:

$(function() { 
      $('.newSongUpload').fileupload({ 
       multipart: true, 
       dataType: 'json'//actually this enough to get plugin works 
       //You can write what will happen after loading in done:yourcode and what you accept in accept:your types 
      }) 
     }); 

的Java Spring代碼:

@RequestMapping(value = {"/upload.new.song"}, method = RequestMethod.POST) 
public @ResponseBody HashMap<String, String> uploadNewSong(HttpServletResponse response, 
           @RequestParam("file") MultipartFile file){ 
//Your code with file here you can save it to the database or to file system with file.getBytes() function 
} 

我希望它會幫助你

+0

感謝大家的幫助。現在我可以從HTML發送文件並將其保存在服務器中。但是我有一個新的查詢,我沒有任何形式,我需要使用java腳本代碼調用這個servlet。我只有那裏可用的blob文件。 我正在添加blob可用的js函數,請讓我知道如何將它發送給servlet? – user3398439

+0

Recorder.setupDownload =函數(blob,文件名){ //代碼保存文件在UI \t var url =(window.URL || window.webkitURL).createObjectURL(blob); var link = document.getElementById(「save」); link.href = url; link.download = filename || 'output.wav'; link.click(); – user3398439

+0

http://stackoverflow.com/questions/13333378/how-can-javascript-upload-a-blob 而不是使用upload.php使用servlet 看來他們將blob轉換爲base64,並在服務器端轉換回 –

0

如果你要處理在Servlet中上傳的文件,文件應該作爲「multipart/form」的請求屬性發送-data「,它應該是POST方法

請參閱Oracle提供的示例。

參考:http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html

+0

所以,我必須使用這個HTML表單代碼發送一個文件到servlet? ..現在有使用JavaScript代碼發送文件到servlet嗎? – user3398439

相關問題