Android Question net lib ftp not Support Chinese file name

hzytsoft

Member
Licensed User
Longtime User
i use net lib
B4X:
Sub Activity_Create(FirstTime As Boolean)
      
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("1")
    If FirstTime Then
        FTP.Initialize("FTP", "192.168.1.103", 21, "zyq", "zyq")
        FTP.List("/")
    End If
End Sub

B4X:
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    Log(ServerPath)
    If Success = False Then
        Log(LastException)
    Else
        For i = 0 To Folders.Length - 1
   
            Log(Folders(i).Name)
        Next
        For i = 0 To Files.Length - 1
        Dim a() As Byte
        a=Files(i).Name.GetBytes("UTF8")
          Dim msg As String
         
        msg=BytesToString(a,0,a.Length,"GBK")
 
       
       
            Log(msg & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
        Next
    End If
End Sub

but Chinese file name is Garbled
 

Attachments

  • QQ图片20140116020543.jpg
    QQ图片20140116020543.jpg
    57.5 KB · Views: 322

netcobra

New Member
Licensed User
Longtime User
I made it!it's my code!
我解决啦,代码如下,希望对你有帮助!
Sub EMenuFTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
Dim i As Int '定义文件名循环
Dim j As Int '定义文件名中乱码字符循环
Dim BConverter As ByteConverter '定义Byte转换库
Dim BytesArray() As Byte '定义Bytes Array
Dim HEXChar As String '定义Bytes 转换为的HEX Char
Dim HEXString As String '定义HEX Char 连接起来的HEXString
Dim GBKString As String '定义HEXString转换完成后的GBK编码String

MenuPhotoName.Initialize '初始化List,否则报错误

If Success = False Then
' Log(LastException)
Msgbox("连接服务器错误!", "错误提示")
ExitApplication

Else

' For i = 0 To Folders.Length - 1 ’获取文件夹
' Log(Folders(i).name)
' Next
'
For i = 0 To Files.Length - 1 '文件名循环

HEXString = ""

For j = 0 To Files(i).Name.Length -1 '文件名取Char循环

BytesArray = BConverter.StringToBytes (Files(i).Name.CharAt(j), "UNICODE") 'Unicode乱码按Char转换为Bytes Array
HEXChar = BConverter.HexFromBytes (BytesArray) 'Bytes 转换为 HEX
HEXString = HEXString & HEXChar.CharAt (4) &HEXChar.CharAt (5) '取出有效的第4位和第5位

Next

BytesArray = BConverter.HexToBytes (HEXString) 'HEXString转换为Bytes Array
GBKString = BConverter.StringFromBytes (BytesArray, "GBK") 'Bytes Array转换为GBK String
MenuPhotoName.Add(GBKString) '加载到MenuName List中

Next

For i=0 To MenuPhotoName.Size-1
'Log(MenuName.Get(i))
' Log(MenuName.Get(i) & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
Next

Msgbox("FTPLIST完成!","提示")

' Msgbox(Files(1).Name & ", " & Files(1).Size & ", " & DateTime.Date(Files(1).Timestamp),"提示")
End If

End Sub
 
Upvote 0

netcobra

New Member
Licensed User
Longtime User
Screen Snapshot
 

Attachments

  • UNICODE乱码.png
    UNICODE乱码.png
    107.2 KB · Views: 311
  • 转换为真实编码的16进制字符串.png
    转换为真实编码的16进制字符串.png
    110.1 KB · Views: 313
  • 16进制字符串转换为GBK编码的中文.png
    16进制字符串转换为GBK编码的中文.png
    106.8 KB · Views: 352
Upvote 0
Top