我试图将视频文件流过静止状态,我试图像这样实现类似泽西的东西:
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;
}
以下是我的文件上传和下载/流媒体(下载半工作,文件大小已损坏):
我真的需要帮助这个人,谢谢
更新
改变:
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;
});
答案 0 :(得分:6)
这是有效的方法。我将上传一个解决方案,让您跳过视频的各个部分,小菜一碟;)
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;
});