我正在使用youtube-api,视频上传示例在我设置时工作正常 video_path字符串放在工作区下面的“/video_name.mp4”。
private static String video_path = "/video_name.MP4";
但是一旦我把它设置为绝对路径
private static String video_path = "C:/Users/Ip300/Desktop/video_name.MP4";
我收到错误
像视频不存在一样。 PS:我在Windows上测试了它正确地重定向到视频的路径。“Throwable:null java.lang.NullPointerException ...”
完整的代码是:
public class UploadVideo {
private static YouTube youtube;
private static final String VIDEO_FILE_FORMAT = "video/*";
private static final String SAMPLE_VIDEO_FILENAME = "video_name.mp4";
private static String video_path = "C:/Users/Ip300/Desktop/video_name.MP4";
public static void main(String[] args) {
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");
try {
Credential credential = Auth.authorize(scopes, "uploadvideo");
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
"youtube-cmdline-uploadvideo-sample").build();
System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);
Video videoObjectDefiningMetadata = new Video();
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoObjectDefiningMetadata.setStatus(status);
VideoSnippet snippet = new VideoSnippet();
Calendar cal = Calendar.getInstance();
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription(
"Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
// Set the keyword tags that you want to associate with the video.
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
videoObjectDefiningMetadata.setSnippet(snippet);
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
UploadVideo.class.getResourceAsStream(video_path));
YouTube.Videos.Insert videoInsert = youtube.videos()
.insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
System.out.println("\n================== Returned Video ==================\n");
System.out.println(" - Id: " + returnedVideo.getId());
System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle());
System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags());
System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
}
答案 0 :(得分:0)
Class.getResourceAsStream()
期待领先&#39; /&#39;为了将路径解释为绝对路径。没有它,路径应该是相对的路径。
而ClassLoader.getResourceAsStream()
仅使用绝对路径,因此不需要领先的&#39; /&#39;。
如果找不到资源,则返回null
。
您使用UploadVideo.class.getResourceAsStream()
方法初始化YouTube.Videos.Insert videoInsert
。
了解更多信息: http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html
注意:如果您使用本机路径并在Windows计算机上工作,则正确的文件分隔符是&#39; \&#39;而不是&#39; /&#39;。
但是对于本机路径(例如c:\ path \ file),则不需要
即可getResourceAsStream()
。您只需使用new FileInputStream("c:\path\file")
;