用sun.net.ftp.FtpClient写了一个客户端,向SUSE Linux服务器上传文件,
但是SUSE Linux服务器编码为UTF-8,而我现在也不能改变该服务器的编码,
所以中文都是?,我怎么能转码呢?sun.net.ftp.FtpClient这个类库里支持中文转码嘛?
谢谢大家.
public void connect(String server, String username, String password,
String path) throws IOException {
client = new FtpClient();
client.openServer(server);
client.login(username, password);
if (path.length() != 0)
client.cd(path);
client.binary();
}
public int upload(String fileName, String newFileName) throws IOException {
int result = 0;
client.sendServer("PASV");
File file = new File(fileName);
if (!file.exists())
return -1;
if (file.length() == 0)
return -2;
TelnetOutputStream tos = client.put(newFileName);
FileInputStream fin = new FileInputStream(file);
byte[] buffer = new byte[1024];
int i;
while ((i = fin.read(buffer)) != -1) {
tos.write(buffer, 0, i);
}
tos.flush();
tos.close();
fin.close();
return result;
}
------解决方案--------------------
你至少知道二进制传输吧!
FTPClient.BINARY_FILE_TYPE
------------------------------------
ftpClient.setControlEncoding("UTF-8")
------解决方案--------------------
处理字符串时转换下编码,如new String(newFileName.getBytes("ISO-8859-1"),"GBK")
总之就是在ISO-8859-1,UTF-9,GBK几个格式之间转换,我用上面的转换已经测试过可以了