简体   繁体   English

如何在网络上访问受密码保护的mp4视频

[英]how to access the mp4 video on web which is password protected

I am using vaadin video to integrate some videos in my application. 我正在使用vaadin视频将一些视频集成到我的应用程序中。 These videos are on web which are password protected. 这些视频在网络上受密码保护。 I am using following approach but its not working for me. 我正在使用以下方法,但不适用于我。 It keeps asking me for username and password. 它一直在问我用户名和密码。

        URL url = new URL(Constants.VIDEO_HTTP_ADDRESS + fileName + ".mp4");
        URLConnection uc = url.openConnection();
        String userpass = Constants.VIDEO_HTTP_ADDRESS_USERNAME + ":" + Constants.VIDEO_HTTP_ADDRESS_PASSWORD;
        String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
        uc.setRequestProperty("Authorization", basicAuth);

....
....
        final Video v = new Video(label);
        ExternalResource fileResource;

        fileResource = new ExternalResource(url);
        v.setSources(fileResource);

I have user name and password, I want to integrate these videos in my application, so that they dont need the username and password explicitly. 我有用户名和密码,我想将这些视频集成到我的应用程序中,以便它们不需要显式的用户名和密码。

As pointed by @f1sh in his comment, you need to use your URLConnection to get the video resource. 正如@ f1sh在其评论中指出的那样,您需要使用URLConnection来获取视频资源。 Instead of ExternalResource you can use StreamResource which allows to load the resource from provided InputStream . 可以使用StreamResource代替ExternalResource ,它允许从提供的InputStream加载资源。

Something like that should work: 这样的事情应该起作用:

Video v = new Video();
v.setSource(new StreamResource(new StreamSource() {

    @Override
    public InputStream getStream() {
        try {
            URL url = new URL(Constants.VIDEO_HTTP_ADDRESS + fileName + ".mp4");
            URLConnection uc = url.openConnection();
            String userpass = Constants.VIDEO_HTTP_ADDRESS_USERNAME + ":" + Constants.VIDEO_HTTP_ADDRESS_PASSWORD;
            String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
            uc.setRequestProperty("Authorization", basicAuth);
            return uc.getInputStream();
        } catch (IOException e) {
            //add some exception handling here
        }
    }

}, fileName + ".mp4"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM