I'm trying to stream a video file over rest, I'm trying to implement something similar to Jersey like this:
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 file upload and download/streaming (download semi-works, the file size is correct by corrupted):
I really need help with this everyone, thanks
UPDATE
changed:
ByteArrayOutputStream out = new ByteArrayOutputStream();
to:
ServletOutputStream out = res.raw().getOutputStream();
UPDATE 2
Ok, I finally got it working, and the video plays in the browser, but now getting a Jetty io.EofException
, I closed the stream but still, must be something simple.
Below are both the before and after:
and downloading the file from the browser works, but how can I stream it directly in the browser?
BEFORE(didn't work)
//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 perfectly, but getting the end of file exception):
get("/post/:id", (req, res ) -> {
//what's 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;
});
Upload:
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;
});
Here is the method that works. I will be uploading a solution that let's you skip to sections of the video as well, piece of cake ;)
get("/post/:id", (req, res ) -> {
//what's 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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With