使用Spark Java通过http流传输视频文件

5

我正在尝试通过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;
        });

抱歉我要挑刺了,但当我看到你提问的方式时,我感到很不舒服。你不能通过架构风格来流式传输视频。 - toniedzwiedz
@toniedzwiedz ??? 好吧,我真的不知道你的意思。无论如何,我大力改进了我的解决方案,现在完美运行。也许我表达不够清楚,但是你的评论其实没什么用处。 - franklinexpress
1个回答

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;

    });

嗨@franklinexpress - 我查看了Sparkjava的Github问题,似乎使用未限定(未知长度)流的方法行不通?这是对的吗?我的具体用例是代理实时音频流(例如http://82.77.137.30:8557/;)。 - yegeniy
1
是的,它应该可以工作。我现在使用带有搜索功能(Accept-Ranges)的Servlet实现了它,如果长度未知,则搜索部分将无法工作,但流式传输应该可以。 - franklinexpress
哦,太好了。那我就不设置长度了。谢谢。 - yegeniy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接