FTP服务器端是UTF-8编码的。我在客户端用VB实现,但是乱码,无法下载中文,具体代码如下。
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
(ByVal sAgent As String, ByVal LAccessType As Long, ByVal sProxyName As String, _
ByVal SProxyBypass As String, ByVal lFlags As Long) As LongPublic Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
(ByVal hInternetSession As Long, ByVal sServerName As String, _
ByVal nServerPort As Integer, ByVal sUsername As String, _
ByVal sPassword As String, ByVal lService As Long, _
ByVal lFlags As Long, ByVal lContext As Long) As LongPublic Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
(ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _
ByVal dwContext As Long) As BooleanPublic Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
(ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _
ByVal dwContext As Long) As Boolean
Private Sub Command2_Click()
    ftpServer = "192.168.163.251"
    user = "admin"
    passwd = "admin"
    port = 2122
    Dim file As String
    file = "补丁文件下载程序-时序图.vsd"
    file = "jftp.jar"
    'file = "补丁升级软件结构图.doc"
    lnginet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, _
       vbNullString, vbNullString, 0&)
    If lnginet Then
      lnginetconn = InternetConnect(lnginet, ftpServer, 2122, _
            user, passwd, 1, 0, 0)   '1 ftp
      If lnginetconn Then
        MsgBox "connect ok!!!"
          blnRC = FtpGetFile(lnginetconn, file, "c:\aaa", 0, 0, FTP_TRANSFER_TYPE_BINARY, 0)
          If blnRC Then
            MsgBox "download ok!!!"
          Else
           MsgBox "download failed!!!"
          End If
          InternetCloseHandle lnginetconn
          InternetCloseHandle lnginet
          MsgBox "close ok!!!"
      Else
         MsgBox "can't connect"
      End If
    Else
         MsgBox "ftp wrong"
    End If
End Sub
我觉得应该是转换为宽字符,用MultiByteToWideChar方法,但具体的还是不清楚。
希望有懂得同学帮忙,多谢!

解决方案 »

  1.   

    尽可能不要用中文文件名,有些网段不支持 8-bit 字符传输。
      

  2.   

    找到问题了。
    FtpGetFile有bug,不能用FtpGetFile。
    替代方案如下:
    To work around this issue, read and write the files manually to implement FTP file exchanges. To do this, follow these steps:
    Open the connection by using InternetOpen, followed by InternetConnect.
    Open the local file by using the CreateFile function.
    Open the remote file by using the FtpOpenFile function.
    Read the FTP or local file by using the InternetReadFile or ReadFile function, respectively.
    Write to FTP or a local file by using the InternetWriteFile or WriteFile function, respectively.
    Close the local file handle by using the CloseHandle function.
    Close the FTP file handle by using the InternetCloseHandle function.