我正在尝试通过rest流式传输视频文件,我正在尝试实现类似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;
}
这里是有关文件上传和下载/流式传输的内容(下载基本可用,但文件大小可能不正确):
大家好,我真的需要帮助,谢谢。
更新
更改为:
ByteArrayOutputStream out = new ByteArrayOutputStream();
to:
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;
});
之后(这个方法可以正常工作,但会出现文件结尾异常):
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;
});