By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,649 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,649 IT Pros & Developers. It's quick & easy.

java.lang.IllegalArgumentException

P: 28
Expand|Select|Wrap|Line Numbers
  1.  public static void download(String url, String fileName) throws Exception {
  2.         String id = url.split("v=")[1].split("&")[0];
  3.         System.out.println(url + " " + id);
  4.         String nid = getDownloadId(id);
  5.         if (nid == null) {
  6.             throw new Exception("Invalud url");
  7.         }
  8.         url = "http://www.youtube.com/watch?v=" + id;
  9.         downloadStreamData(url, fileName);
  10.     }
  11.     /*
  12.      * returns the id of the video data
  13.      */
  14.     private static String getDownloadId(String videoId) throws MalformedURLException, IOException {
  15.         String url = "http://www.youtube.com/watch?v=" + videoId;
  16.         URL tU = new URL(url);
  17.         HttpURLConnection conn = (HttpURLConnection) tU.openConnection();
  18.         InputStream ins = conn.getInputStream();
  19.         BufferedReader rd = new BufferedReader(new InputStreamReader(ins));
  20.         String line;
  21.         StringBuffer content = new StringBuffer();
  22.         while ((line = rd.readLine()) != null) {
  23.             content.append(line);
  24.         }
  25.         String sContent = URLDecoder.decode(content.toString(), "UTF-8");
  26.         String[] tokens = sContent.split("&");
  27.         for (int i = 0; i < tokens.length - 1; i++) {
  28.             if (tokens[i] == null) {
  29.                 continue;
  30.             }
  31.             String param1 = tokens[i].substring(0, tokens[i].indexOf("="));
  32.             String param2 = tokens[i].substring(param1.length() + 1);
  33.             if (param1.equals("token")) {
  34.                 return param2;
  35.             }
  36.         }
  37.         return "";
  38.     }
  39. /*
  40.      * this function will download the file after the stream is found
  41.      */
  42.     private static void downloadStreamData(String url, String fileName) throws Exception {
  43.         URL tU = new URL(url);
  44.         HttpURLConnection conn = (HttpURLConnection) tU.openConnection();
  45.  
  46.         String type = conn.getContentType();
  47.         InputStream ins = conn.getInputStream();
  48.         FileOutputStream fout = new FileOutputStream(new File(fileName));
  49.         byte[] outputByte = new byte[4096];
  50.         int bytesRead;
  51.         int length = conn.getContentLength();
  52.         int read = 0;
  53.         while ((bytesRead = ins.read(outputByte, 0, 4096)) != -1) {
  54.             read += bytesRead;
  55.             System.out.println(read + " out of " + length);
  56.             fout.write(outputByte, 0, bytesRead);
  57.         }
  58.         fout.flush();
  59.         fout.close();
  60.     }
  61.  
  62.  

I get this error on the console when I run it:
http://www.youtube.com/watch?v=jUEOWVjnIR8 jUEOWVjnIR8
Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "">"
at java.net.URLDecoder.decode(Unknown Source)
at youtube.file.YouTube.getDownloadId(YouTube.java:52 )
at youtube.file.YouTube.download(YouTube.java:30)
at youtube.file.YouTube.main(YouTube.java:21)
Apr 20 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
The string you are trying to decode is illegal according to the rules specified here http://docs.oracle.com/javase/6/docs...RLDecoder.html
Apr 22 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.