java ftp上传文件
//这边path指的是相对路径,需要切换到,path目录,然后
public static void upload(String server, int port, String user, String pwd, List<String> sourcePaths, List<String> desPaths, String filename) {
if (sourcePaths.size() == 0 || desPaths.size() == 0) {
return;
}
if (sourcePaths.size() != desPaths.size()) {
System.out.println("源文件地址与目标地址不匹配!");
}
FTPClient client = new FTPClient();
FileInputStream inputStream = null;
try {
client.connect(server, port);
client.login(user, pwd);
for (int i = 0; i < sourcePaths.size(); i++) {
inputStream = new FileInputStream(sourcePaths.get(i));
client.changeWorkingDirectory(desPaths.get(i));
boolean b = client.storeFile(filename, inputStream);
if (b == false) {
System.out.println("上传文件:" + sourcePaths.get(i) + "失败!目标主机:" + server);
}
}
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency>