Upload / stream video using Java servlet

I am trying to upload a video file on my server when the client access url is like:

http://localhost:8088/openmrs/moduleServlet/patientnarratives/videoDownloadServlet?videoObsId=61 

I tried this code. But that does not work. When I am in the servlet, it only loads the empty file (0 size).

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Integer videoObsId = Integer.parseInt(request.getParameter("videoObsId")); Obs complexObs = Context.getObsService().getComplexObs(videoObsId, OpenmrsConstants.RAW_VIEW); ComplexData complexData = complexObs.getComplexData(); Object object2 = complexData.getData(); // <-- an API used in my service. this simply returns an object. byte[] videoObjectData = SerializationUtils.serialize(object2); // Get content type by filename. String contentType = null; if (contentType == null) { contentType = "application/octet-stream"; } // Init servlet response. response.reset(); response.setBufferSize(DEFAULT_BUFFER_SIZE); response.setContentType(contentType); response.setHeader("Content-Length", String.valueOf(videoObjectData.length)); response.setHeader("Content-Disposition", "attachment; filename=\"" + "test.flv" + "\""); // Prepare streams. BufferedInputStream input = null; BufferedOutputStream output = null; try { // Open streams. input = new BufferedInputStream(new ByteArrayInputStream(videoObjectData), DEFAULT_BUFFER_SIZE); output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE); // Write file contents to response. byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally { // Gently close streams. close(output); close(input); } } // Add error handling above and remove this try/catch catch (Exception e) { log.error("unable to get file", e); } } private static void close(Closeable resource) { if (resource != null) { try { resource.close(); } catch (IOException e) { // Do your thing with the exception. Print it, log it or mail it. e.printStackTrace(); } } } 

I used the BalusC tutorial on the file system , but in my case I don't have a file object as the input stream of just an object of an array of bytes.

help..

+6
source share
1 answer

The servlet you found is really not designed to stream a video file. It is more conceived as a simple file upload servlet for static files such as PDF, XLS, etc.

Many video players require the server to support so-called HTTP range requests. That is, it should be able to return a certain range of bytes of the video file upon request with the Range header. For example, only bytes from index 1000 to 2000 in a file 10,000 bytes long. This is necessary in order to be able to skip a certain range of the video stream quickly enough, without having to download the entire file and / or improve the buffering speed, creating several HTTP connections, each of which requests a different part of the video file.

However, there is a lot of extra code in the servlet that requires a good understanding of the HTTP Range specification. A ready-to-use example is presented in the style of this extended file servlet by the same author of the file servlet you found. In your specific case, it is probably recommended that you first save the file in the local cache based on the local file system (for example, via File#createTempFile() and some key in an HTTP session), so you do not need to get it again and again from the external service.

+7
source

Source: https://habr.com/ru/post/952994/


All Articles