i'm using youtube-api, video upload sample works fine when set video_path string "/video_name.mp4" placed under workspace .
private static string video_path = "/video_name.mp4";
but once set absolute path private static string video_path = "c:/users/ip300/desktop/video_name.mp4";
i error " throwable: null java.lang.nullpointerexception...", video doesn't exist. ps : tested path on windows redirects correctly video.
the full code :
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 java library " + "on " + cal.gettime()); // set keyword tags want associate 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(); } }
}
class.getresourceasstream()
expects leading ' / ' in order interpret path absoute path. without path supposed relative one. whereas classloader.getresourceasstream()
uses absolute paths, there no need leading ' / '.
both return null
in case resource not found.
you use uploadvideo.class.getresourceasstream()
method initialize youtube.videos.insert videoinsert
.
for further information: http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html
note: if use native path , work on windows machine, proper file seperator ' \ ' instead of ' / '.
however native paths ( e.g. c:\path\file ) there no need
getresourceasstream()
. can usenew fileinputstream("c:\path\file")
;
Comments
Post a Comment