java ftp 上传 中文_java往ftp服务器上传文件,该文件内有中文。结果上传到ftp服务器上中文乱码,请问这个怎么解决?...

您好,您可以试下以下代码,设置为UTF-8的代码已加粗

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

public class Test {

private FTPClient ftp;

public static void main(String[] args) throws SocketException, IOException {

Test t = new Test();

t.connect("", "localhost", 21);

File file = new File("E:\\test.txt");

t.upload(file);

}

private boolean connect(String path,String addr,int port) throws SocketException, IOException {

boolean result = false;

ftp = new FTPClient();

int reply;

ftp.connect(addr,port);

ftp.login("anonymous", "");

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

System.out.println(result);

return result;

}

ftp.changeWorkingDirectory(path);

result = true;

System.out.println(result);

return result;

}

private void upload(File file) throws IOException {

ftp.setControlEncoding("utf-8");

if(file.isDirectory()){

ftp.makeDirectory(file.getName());

ftp.changeWorkingDirectory(file.getName());

String[] files = file.list();

for (int i = 0; i < files.length; i++) {

File file1 = new File(file.getPath()+"\\"+files[i] );

if(file1.isDirectory()){

upload(file1);

ftp.changeToParentDirectory();

}else{

File file2 = new File(file.getPath()+"\\"+files[i]);

FileInputStream input = new FileInputStream(file2);

ftp.storeFile(file2.getName(), input);

input.close();

}

}

}else{

File file2 = new File(file.getPath());

FileInputStream input = new FileInputStream(file2);

ftp.storeFile(file2.getName(), input);

input.close();

}

}

}

如您还有疑问或有不赞同的地方,欢迎追问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值