我正在尝试在休息时流式传输视频文件,我正在尝试实现类似于 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();
至:
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;
});