我的表单使用 XMLHttpRequest 作为 POST 请求发送视频和音频 blob
在服务器端 Spring 控制器——我想检索 HTTP 请求并传递这两个 blob 并将其作为两个文件保存到硬盘。
有人可以帮我解决这个问题。谢谢!
这是我的 JS 代码
function sendAudioVideoBlobs(){
console.log("Sending blobs!");
var recordForm = new FormData();
recordForm.append("video", videoBlob);
recordForm.append("audio", audioBlob);
var oReq = new XMLHttpRequest();
formSubmitURL = "http://localhost:8088/openmrs/module/patientnarratives/webRtcMedia.form";
console.log(formSubmitURL);
oReq.open("POST", formSubmitURL);
oReq.send(recordForm);
}
控制器类
@Controller
public class WebRtcMediaStreamController {
private String returnUrl;
public final static String FORM_PATH = "/module/patientnarratives/webRtcMedia.form";
protected final Log log = LogFactory.getLog(getClass());
private String audioUrl = "/home/audioFile1.wav";
private String videoUrl = "/home/videoFile1.webm";
@RequestMapping(FORM_PATH)
public ModelAndView handleRequest(HttpServletRequest request) throws Exception {
Integer encId = Integer.parseInt(request.getParameter("textEncID"));
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile videofile = (MultipartFile) multipartRequest.getFile("video");
MultipartFile audiofile = (MultipartFile) multipartRequest.getFile("audio");
byte[] videoData = videofile.getBytes();
byte[] audioData = audiofile.getBytes();
if (!videofile.isEmpty()) {
if (videofile.getSize() > 0) { // limit video length or size.
OutputStream out1 = new FileOutputStream(new File(videoUrl));
out1.write(videoData);
out1.close();
OutputStream out2 = new FileOutputStream(new File(audioUrl));
out2.write(audioData);
out2.close();
}
}
}
returnUrl = request.getContextPath() + "/module/patientnarratives/patientNarrativesForm.form";
return new ModelAndView(new RedirectView(returnUrl));
}