您好,您可以试下以下代码,设置为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();
}
}
}
如您还有疑问或有不赞同的地方,欢迎追问。