将音频文件/blob从UI发送到Servlet,以保存在服务器上. [英] Sending Audio file/blob from UI to Servlet for saving at server.

查看:35
本文介绍了将音频文件/blob从UI发送到Servlet,以保存在服务器上.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用以下代码从UI端发送音频文件

We are trying to send our Audio file from the UI side using following code

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中.预先感谢.

But we are unable to process that in servlet. Can anybody help me with Javascript code to send audio file to servet and servlet code to save that audio fie in the servlet. Thanks in advance.

推荐答案

美好的一天

您可以使用插件文件上传

You can use a plugin fileupload

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

关于如何在spring和ajax中使用它的说明很充分:

There is quite full instruction how to use it with spring and ajax:

http://krams915.blogspot.ru/2012/06/file-upload-with-spring-and-jquery-part_2793.html (来自此插件的Wiki)

http://krams915.blogspot.ru/2012/06/file-upload-with-spring-and-jquery-part_2793.html (from wiki of this plugin)

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

Quick tutorial (don't forget to include the plugin)
Html code:

<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春季代码:

  @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
}

希望对您有帮助

这篇关于将音频文件/blob从UI发送到Servlet,以保存在服务器上.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆