我们正尝试使用以下代码从UI端发送我们的音频文件
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代码将音频文件发送到servet和servlet代码,以便将该音频文件保存在servlet中。提前谢谢。
答案 0 :(得分:1)
美好的一天
您可以使用插件fileupload
https://github.com/blueimp/jQuery-File-Upload
有关如何使用spring和ajax的完整说明:
http://krams915.blogspot.ru/2012/06/file-upload-with-spring-and-jquery-part_2793.html(来自此插件的wiki)
快速教程(不要忘记包含插件)
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
}
我希望它会帮助你
答案 1 :(得分:0)
如果要处理Servlet中上传的文件,文件应作为&#34; multipart / form-data&#34;的请求属性发送。它应该是POST方法
请参阅Oracle提供的示例。