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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…