Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

java - Sending a blob to a servlet through ajax

EDIT: 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.

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);
}

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();

It saves the file as well.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some ideas:

Increase the buffer size: Perhaps:

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

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.

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()

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...