I am trying to transfer a video file through rest, I am trying to implement something similar to Jersey as follows:
ResponseBuilder builder = Response.ok(out.toByteArray()); builder.header("Content-Disposition", "attachment; filename=" + fields.get("filename")); response = builder.build(); } else { response = Response.status(404). entity(" Unable to get file with ID: " + id). type("text/plain"). build(); } return response; }
Here is what I have for uploading files and uploading / streaming (uploading semi-finished products, file size is correct due to corruption):
I really need help with this, thanks
UPDATE
changes:
ByteArrayOutputStream out = new ByteArrayOutputStream();
in
ServletOutputStream out = res.raw().getOutputStream();
UPDATE 2 Okay, I finally started working and the video is playing in the browser, but now, having received Jetty io.EofException , I closed the stream, but it should still be something simple.
Below are both before and after values:
and downloading the file from the browser works, but how can I transfer it directly to the browser?
BEFORE (not working)
//download a video/ trying to stream it right in the browser if possible get("/post/:id", (req, res ) -> { res.raw().setContentType("application/octet-stream"); String id = req.params(":id"); ObjectId objectId = new ObjectId(id); BasicDBObject query = new BasicDBObject(); query.put("_id", objectId); //DBObject video = collection.findOne(query); GridFS gridfile = new GridFS(db, "videos"); GridFSDBFile gridFSDBFile = gridfile.findOne(query); res.raw().setHeader("Content-Disposition", "attachment; filename=" + gridFSDBFile.getFilename()); InputStream inputStream = gridFSDBFile.getInputStream(); ServletOutputStream out = res.raw().getOutputStream(); // ByteArrayOutputStream out = new ByteArrayOutputStream(); int data = inputStream.read(); while (data >= 0) { out.write((char) data); data = inputStream.read(); } out.flush(); out.close(); return out; });
AFTER (this works fine, but the file exception ends):
get("/post/:id", (req, res ) -> { //what the difference between these 2? res.raw().setContentType("video/mp4"); res.type("video/mp4"); String id = req.params(":id"); ObjectId objectId = new ObjectId(id); BasicDBObject query = new BasicDBObject(); query.put("_id", objectId); GridFS gridfile = new GridFS(db, "videos"); GridFSDBFile gridFSDBFile = gridfile.findOne(query); res.raw().setContentLengthLong(gridFSDBFile.getLength()); InputStream inputStream = gridFSDBFile.getInputStream(); ServletOutputStream out = res.raw().getOutputStream(); int data = inputStream.read(); while (data >= 0) { gridFSDBFile.writeTo(out); data = inputStream.read(); } // out.flush(); out.close(); return 200; });
Download:
post("/postvideo/:username", (req, res) -> { MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp"); req.raw(). setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement); String username = req.params(":username"); double[] location = new double[2]; double lattitude = Double.parseDouble(req.queryParams("lat")); double longitude = Double.parseDouble(req.queryParams("lon")); location[0] = lattitude; location[1] = longitude; InputStream inputStream = req.raw().getPart("file").getInputStream();; Part uploadedFile = req.raw().getPart("file"); // File file = new File(uploadedFile.getName()); GridFS gridFS = new GridFS(db, "videos"); GridFSInputFile gfsFile = gridFS.createFile(inputStream); gfsFile.put("location", location); gfsFile.put("username", username); gfsFile.put("contentType", req.raw().getContentType()); gfsFile.put("filename", uploadedFile.getSubmittedFileName()); collection.insert(gfsFile); gfsFile.save(); return 201; });