我正在使用 vaadin video 将一些视频集成到我的应用程序中。 这些视频位于网络上,受密码保护。我正在使用以下方法,但它对我不起作用。它一直询问我用户名和密码。
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);
我有用户名和密码,我想将这些视频集成到我的应用程序中,这样它们就不需要明确地需要用户名和密码。
最佳答案
正如 @f1sh 在他的评论中指出的,您需要使用 URLConnection
来获取视频资源。您可以使用 StreamResource
来代替 ExternalResource
,它允许从提供的 InputStream
加载资源。
类似的东西应该有效:
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"));
关于java - 如何访问受密码保护的网络上的 mp4 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43791282/