Import java.net.*; Import java.io.*; public class Mldonkeyutil { /** * MLDonkey File Download store path */ Private String incoming = "/usr/local/mldonkey/incoming"; public static void Main (string[] args) throws Exception { Mldonkeyutil util = new Mldonkeyutil (); Util.convert (); } private void convert () { File dir = new file (incoming); file[] files = dir.listfiles (); for (int i = 0; i < files.length; i++) { String strdest = Convert (Files[i].getname ()); if (!files[i].getname (). Equals (strdest)) { Print (Files[i].getname () + ">>>" + strdest); File Fdest = new file (files[i].getparent () + file.separator + strdest); Files[i].renameto (fdest); } } } Private String Convert (string s) { int location = 0; String ret = ""; while (true) { if (location + 8 <= s.length ()) { String subStr = s.substring (location, location + 8); if (check (SUBSTR)) { RET + + "%" + Convert2hex (substr.substring (1, 4)) + "%" + Convert2hex (substr.substring (5, 8)); Location = location + 8; } else { RET + s.substring (location, location + 1); Location = location + 1; } } else { RET + s.substring (location); break; } } Return Urldecoder.decode (ret); } /** * Check match type (_NNN_NNN) * @param s * @return */ Private Boolean check (String s) { if (S.length ()!= 8) { return false; } if (S.charat (0)!= ' _ ' | | S.charat (4)!= ' _ ') { return false; } if (Isnum (S.charat (1)) && Isnum (S.charat (2)) && Isnum (S.charat (3)) && Isnum (S.charat (5)) && Isnum (S.charat (6)) && Isnum (S.charat (7)) { return true; } return false; } Private Boolean Isnum (char c) { if (c >= && C <= 57) { return true; } return false; } private String Convert2hex (string s) { int i = Integer.parseint (s); if (I < 0 | | | i > 255) { throw new IllegalArgumentException (); } return integer.tohexstring (i); } private void print (String str) { System.out.println (str); } } |