MLDonkey: how to download Chinese files with garbled characters

Source: Internet
Author: User

MLDonkeyHow to download Chinese files with garbled characters

 

Author:Kongxx

MLDonkey is currently a popular Donkey in Linux, but its core class library cannot handle Chinese well, which also makes many Chinese tech-support professionals discouraged, the following is a method for processing files in the MLDonkey process that contains Chinese characters. However, this method only performs conversion after the files are downloaded, and Chinese characters are still unavailable during the download process, I will try to solve this problem in the future:

Note: The current version only supports encoding a Chinese character into two bytes when adding a file.

This method uses a class written in Java. The Code is as follows:

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 );

}

}

Change incoming in this class to the MLDonkey incoming directory, and then enter the following command in the Linux Command Line for compilation and execution.

Compile

# Javac MLDonkeyUtil. java

Run

# Java MLDonkeyUtil

If there is a file in the directory that needs to change the Chinese name, this program will automatically convert and output the name of the file before and after conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.