A problem caused by a Chinese file name

Source: Internet
Author: User

There is an FTP component in the project. When downloading a file named in Chinese, the error "access is denied, cannot be found the file specified" is always thrown, but the file named in English can be downloaded normally. The first thing that comes to mind is the encoding problem. If you urlencode the file name once, it is still the same error. Search laterCodeNon-standard FTP command is used.

Let's take a look at the download code:

1 Public   Void Opendownload ( String Remotefilename, String Localfilename, Bool Resume ){
2 ** * ******** Code etc ************ **
3 Setbinarymode ( True );
4 Opendatasocket ();
5
6 Bytes_total =   0 ;
7
8 _ Filesize = Getfilesize (remotefilename );
9
10 Byte [] cmd = Encoding. utf8.getbytes (( " RETR " ). Tochararray ());
11 Byte [] Bytes = Encoding. utf8.getbytes (remotefilename );
12 Byte [] Content = Encoding. Convert (encoding. utf8, encoding. Default, bytes );
13 Int Totallength = Cmd. Length + Content. length;
14 Byte [] Re =   New   Byte [Totallength +   2 ];
15 For ( Int I =   0 ; I < Cmd. length; I ++ )
16 {
17 Re [I] = CMD [I];
18 }
19 For ( Int I =   0 ; I < Content. length; I ++ )
20 {
21 Re [cmd. Length + I] = Content [I];
22 }
23 Re [totallength] =   13 ;
24 Re [totallength +   1 ] =   10 ;
25
26 Main_sock.send (Re, re. length, 0 );
27
28 Readresponse ();
29 ** * ******** Code etc ************ **
30 }

 

The statement that throws the problem is marked below. Obtain the file size on the remote server. Code:

Get File Size

1 ///   <Summary>
2 /// Get File Size
3 ///   </Summary>
4 ///   <Param name = "FILENAME"> File Name </Param>
5 ///   <Returns> File Size </Returns>
6 Public   Long Getfilesize ( String Filename ){
7 Connect ();
8 Sendcommand ( " Size "   + Filename );
9 Readresponse ();
10
11 If (Response ! =   213 ) Throw   New Serverexception (responsestr );
12
13 Return Int64.parse (responsestr. substring ( 4 ), Cultureinfo. currentculture );
14 }

The sendcommand function is

1 Private   Void Sendcommand ( String Command ){
2 Byte [] cmd = Encoding. ASCII. getbytes (command +   " \ R \ n " ). Tochararray ());
3 Main_sock.send (CMD, cmd. length, 0 );
4 }

After socked sends the ftp size command, the response will never be 213. The exception thrown is that the specified file cannot be found, regardless of whether the file name has been unicode encoded.

In the sendcommand function, we can see that the command is converted into bytes as an ASCII string. To Unicode? The server will throw a timeout error: "timed out waiting on server to respond ". Utf8? The server will throw the same error as the ASCII code.

Later, I checked related documents and found that the FTP SIZE command is a non-standard command. In the ASCII coding system, the size command is supported to obtain the file size. Some unix ftp services do not support non-standard commands. Size exists in the ASCII encoding system. Therefore, if a non-ASCII file name is obtained by using size, the Server Returns Error 550: "access is denied, cannot be found the file specified ".

The problem is finally found. The Chinese characters are not in the ASCII code range. The size command is sent and the FTP server (non-standard FTP command is supported; otherwise, the FTP server returns the 500 error: Unrecognized command) the file name will be parsed as an ascii code, and the Chinese file name cannot be found. After urlecode is performed, the file name is changed after a series of bytes are converted and sent to the FTP server, naturally, files cannot be found.

The next step is the modification problem. The calculation of the file size is placed after the file is downloaded to the local device, but not before the download. At the same time, the encoding is unified to uft8. Now the solution is complete.

 

 

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.