Import java.net .*; Import java. io .*; Public class MLDonkeyUtil { /** * Mldonkey File Download and storage 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 the matching 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> = 48 & 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 ); } } |