如何在 Struts 2 中将音频 blob(数据)文件从 JavaScript 发送到 Java 服务器操作? [英] How to send an audio blob (data) file from JavaScript to Java server action in Struts 2?

查看:25
本文介绍了如何在 Struts 2 中将音频 blob(数据)文件从 JavaScript 发送到 Java 服务器操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Struts2 应用程序,我想将录制的 JavaScript 音频 blob 文件发送到 Java 服务器(Action 类).我正在使用以下 Ajax 代码发送 blob 文件.

JavaScript:

 var xhr=new XMLHttpRequest();xhr.onload=function(e) {if(this.readyState === 4) {console.log("服务器返回:",e.target.responseText);}};var fd=new FormData();fd.append("filename.wav",blob);xhr.open(POST",getAudio",true);xhr.send(fd);

我在 Action 类中的方法是 getAudio().所以,既然它是一个数据文件,我如何在我的方法中接收它并建议适当的 XML 配置来接收它.

解决方案

在 Struts2 中,您可以为发送文件时将调用的操作创建 xml 配置.

<result>success.jsp</result></动作>

该类应包含映射到操作的方法以及文件及其名称的属性.

public class UploadAction extends ActionSupport {私人字符串名称;私人档案档案;//getter 和 setter公共文件 getFile() {返回文件;}公共无效集文件(文件文件){this.file = 文件;}公共字符串 getName() {返回名称;}公共无效集名称(字符串名称){this.name = 名称;}公共字符串上传()抛出异常{Files.move(file.toPath(),Paths.get("C:\\tmp\\"+name), StandardCopyOption.REPLACE_EXISTING);返回成功;}}

现在你应该把属性放在表单数据对象中

var xhr=new XMLHttpRequest();xhr.onload=function(e) {if(this.readyState === 4) {console.log("服务器返回:",e.target.responseText);}};var fd=new FormData();fd.append("name", "filename.wav");fd.append("file", blob);xhr.open("POST","getAudio",true);xhr.send(fd);

<块引用>

重要的是要知道,为了能够在服务器端处理 FormData 表单,它与使用 multipart 编码发送的常规表单相同/form-data.

这对于 Struts2 fileUpload 拦截器很重要,以便在调用操作时能够处理它.

为了进一步学习,我建议您阅读如何异步上传文件?.

We are using Struts2 Application where in I want to send the recorded audio blob file which is in JavaScript, to Java Server (Action class). I am using the following Ajax code to send the blob file.

JavaScript:

  var xhr=new XMLHttpRequest();
  xhr.onload=function(e) {
      if(this.readyState === 4) {
          console.log("Server returned: ",e.target.responseText);
      }
  };
  var fd=new FormData();
  fd.append("filename.wav",blob);
  xhr.open("POST","getAudio",true);
  xhr.send(fd);

My method in Action class is getAudio(). So, since it is a data file, how can I receive it in my method and suggest the appropriate XML configuration to receive the same.

解决方案

In Struts2 you can create xml configuration for the action that will be invoked when you send a file.

<action name="getAudio" class="com.example.UploadAction" method="upload">
  <result>success.jsp</result>
</action>

The class should include a method mapped to the action and a properties for the file and its name.

public class UploadAction extends ActionSupport {
  private String name;
  private File file;

  //getter and setter

  public File getFile() {
    return file;
  }

  public void setFile(File file) {
    this.file = file;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String upload() throws Exception{
      Files.move(file.toPath(),Paths.get("C:\\tmp\\"+name), StandardCopyOption.REPLACE_EXISTING);
      return SUCCESS;
  }
}

Now you should put properties in the form data object

var xhr=new XMLHttpRequest();
xhr.onload=function(e) {
   if(this.readyState === 4) {
       console.log("Server returned: ",e.target.responseText);
   }
};
var fd=new FormData();
fd.append("name", "filename.wav");
fd.append("file", blob);
xhr.open("POST","getAudio",true);
xhr.send(fd);

What is important to know, to be able to deal on the server side with a FormData form, is that it is the same as regular form that has been sent with the encoding multipart/form-data.

And this is important for Struts2 fileUpload interceptor to be able to handle it when the action is invoked.

For the further learning I recommend you to read How can I upload files asynchronously?.

这篇关于如何在 Struts 2 中将音频 blob(数据)文件从 JavaScript 发送到 Java 服务器操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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