通过 ajax 将 blob 发送到 servlet

Sending a blob to a servlet through ajax(通过 ajax 将 blob 发送到 servlet)
本文介绍了通过 ajax 将 blob 发送到 servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个问题原来是网络问题,但如果您看到任何关于如何优化流程的想法,我仍然会很感激.

The whole problem turned out to be a network issue, but if you see any ideas on how I could optimize the process I'd still appreciate it.

我对 Servlet 还很陌生,在我的旅程中不久,我遇到了一个与性能有关的问题.我正在尝试通过 Google Chrome 浏览器中的 XHR 对象发送视频文件.视频文件存储在 Blob 对象中.我在我的 JavaScript 脚本中使用了这个函数:

I am fairly new to Servlets and not far into my journey I have encountered a problem, one related to performance. I am trying to send a video file through the XHR object in my Google Chrome browser. The video file is stored in a Blob object. I use this function in my JavaScript script:

function upload(blob) {
  var xhr = new XMLHttpRequest();
  xhr.open('POST', '/Test/Odbieracz', true);
  xhr.onload = function(e) { console.log("loaded"); };
  xhr.onreadystatechange = function(){
      console.log("state: " + xhr.readyState);
  };
  // Listen to the upload progress.
  xhr.upload.onprogress = function(e) { console.log("uploading..."); };
  xhr.setRequestHeader("Content-Type", "video/webm");
  xhr.send(blob);
}

效果很好,因为 Blob 到达了我使用这段代码处理它的 Servlet:

It works well, because the Blob reaches the Servlet where I use this bit of code to process it:

byte[] buffer = new byte[16 * 1024];

InputStream input = request.getInputStream();       
OutputStream output = new FileOutputStream("costam0.webm");
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1){
    System.out.println(bytesRead);
    output.write(buffer, 0, bytesRead);
}
output.close();
input.close();

它也会保存文件.

我确实遇到的问题是它非常非常慢,根据我的计算它可以处理大约 42kB/s,这对于与视频文件有关的 Web 服务来说非常慢.我已经在这里坐了好几个小时,试图找到一种方法来加快速度,或者至少找到瓶颈,但不幸的是我不知道它可能在哪里.

The issue I do have is that it is very very slow, according to my calculations it can process about 42kB/s, which for a web service having to do with video files is extremely slow. I have been sitting here for hours trying to find a way to speed it up somehow, or to at least find the bottleneck, but unfortunately I have no idea where it might be.

我怀疑是浏览器导致了延迟,我在我的 Servlet 中使用了不同的 InputStream 导致了一个本地文件(我试图通过 XHR 上传的同一个文件)并且它在处理它时完全没有问题,不到一秒钟.服务器驻留在我的本地主机上,所以我认为网络一点也不落后.

My suspicion is that the browser is causing the lag, I have used a different InputStream in my Servlet leading to a local file (the same one I'm trying to upload through XHR) and it had no issue processing it at all, took less than a second. The server is stationed on my localhost, so I don't think the network is lagging me much at all.

如果有人以前遇到过这个问题,我会很感激任何指点.

If anyone had this issue before, I'd be grateful for any pointers.

推荐答案

一些想法:

增加缓冲区大小:也许:

Increase the buffer size: Perhaps:

byte[] buffer = new byte[1024 * 1024];

不要经常写输出文件.Java 必须在缓冲区的其余部分等待时执行昂贵的 I/O 操作.权衡是,如果您正在处理小文件,您将浪费一些内存.

Don't write the output file as often. Java has to perform expensive I/O operations while the rest of the buffer is waiting. The tradeoff is that if you are dealing with small files, you will have wasted a bit of memory.

使用 BufferedOutputStream:原因同上.强烈建议在编写非常大的文件时使用 BufferedOutputStream.您甚至不必担心在每个循环期间进行编写.只需在循环完成后调用 buffOut.flush() 进行一次写入.示例:

Use a BufferedOutputStream: Same reasons as above. It is highly recommended to use BufferedOutputStream when writing very large files. You don't have to even worry about writing during each loop. Just call buffOut.flush() to make a single write after the loop is complete. Example:

BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream("costam0.webm"));       
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1){
    System.out.println(bytesRead);
    output.write(buffer, 0, bytesRead);
}
buffOUt.flush()

您是否在调用 println 时计算了这些计算?不要那样做.您确认该过程有效,只是速度很慢.无需每次都调用 println.

Did you compute those calculations while calling println? Don't do that. You confirmed that process works, just slow. No need to call println every time.

另外,你是如何计算"速度的?

Also, how did you "calculate" the speed?

这篇关于通过 ajax 将 blob 发送到 servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)