[英]Stream a video file over http with Spark Java
我正在嘗試將視頻文件流過靜止,我正在嘗試像這樣實現類似於Jersey的東西:
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;
}
這是我的文件上傳和下載/流媒體(下載半工作,文件大小已損壞):
我真的需要幫助這個人,謝謝
UPDATE
改變:
ByteArrayOutputStream out = new ByteArrayOutputStream();
至:
ServletOutputStream out = res.raw().getOutputStream();
更新2好的,我終於讓它工作了,視頻在瀏覽器中播放,但現在得到一個Jetty io.EofException
,我關閉了流,但仍然,必須是簡單的東西。
以下是前后:
並從瀏覽器下載文件,但如何直接在瀏覽器中流式傳輸?
之前(沒工作)
//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(這很好用,但是文件異常結束):
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;
});
上傳:
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;
});
這是有效的方法。 我將上傳一個解決方案,讓你跳過視頻的部分,一塊蛋糕;)
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;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.