原来用enterprisedt Ftp Client写一个ftp上传的功能,客户测试时发现中文文件名的上传上去乱码
试过很多办法解决不了乱码问题,于是放弃了
改用sun.net.ftp.FtpClient
代码如下:
import java.io.FileInputStream;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class FtpUpload {
public static void main(String[] args) throws IOException {
FtpClient fc = new FtpClient("10.8.1.76", 21);
fc.login("user","pwd");
fc.cd("aaa/bbb");
fc.binary();
upload(fc, "E://测试.txt", "测试.txt");
fc.closeServer();
}
public static void upload(FtpClient fc, String in, String out) throws IOException {
FileInputStream fis = new FileInputStream(in);
TelnetOutputStream tos = fc.put(out);
int readLength = 0;
byte[] bytes = new byte[1024];
while ( (readLength = fis.read(bytes)) != -1) {
tos.write(bytes, 0, readLength);
}
fis.close();
tos.close();
}
}
* format the number to LL...LVAR
* size is the length of LL...L
*
* @throws Exception
*
*/
public static byte[] zfill(int num, int size) throws Exception {
if(num>(int)Math.pow(10, size)-1)
throw new Exception("the number is too big");
byte[] buf = new byte[size--];
byte z =(byte) '0';
while(num>=10) {
buf[size--] = (byte) (num%10+z);
num = num/10;
}
buf[size] = (byte)(num+z);
for(int i=0; i<size; i++)
buf[i]=(byte)'0';
return buf;
}