2017-04-05 110 views
1

我使用YouTube的API,將視頻上傳樣本當我設置工作區下放置 video_path字符串「/video_name.mp4」工作正常。視頻路徑字符串與YouTube-API

私人靜態字符串video_path = 「/video_name.MP4」;

但是,一旦我將它設置爲絕對路徑 private static String video_path =「C:/Users/Ip300/Desktop/video_name.MP4」;

我得到錯誤「的Throwable:空顯示java.lang.NullPointerException ...」,如視頻不存在。 PS:我在Windows上測試了它正確重定向到視頻的路徑。

的完整代碼是:

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

我不知道我們應該如何幫助沒有任何代碼。 – tnw

+0

可能的重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – tnw

回答

0
Class.getResourceAsStream()

預計領先爲了解釋的路徑作爲路徑absoute '/'。沒有它,路徑應該是相對的。 而ClassLoader.getResourceAsStream()只使用絕對路徑,所以不需要前導'/'。

兩個返回null的情況下,沒有被發現的資源。

您使用UploadVideo.class.getResourceAsStream()方法來初始化YouTube.Videos.Insert videoInsert

欲瞭解更多信息: http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html

注:如果使用本地路徑和工作在Windows機器上,適當的文件分隔符是「\」,而不是「/」。

但是對於本機路徑(例如c:\ path \ file),不需要 getResourceAsStream()。您可以簡單地使用new FileInputStream("c:\path\file");

+0

我的目標是從fileChooser獲得絕對路徑,然後將其作爲video_path變量傳遞。絕對路徑的輸出是「C:/Users/Ip300/Desktop/video_name.MP4」,即使是前導'/' – kingabdr

+0

@kingabdr也不行,對於相對,絕對路徑和本地路徑存在誤解。我更新了答案。 –