JAVA 上传文件到FTP
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class FTPUpload {
public static void main(String[] args) {
String projectRoot = System.getProperty("user.dir");
String filePath = Paths.get(projectRoot, "config/application3ke.json").toString();
JSONObject obj=JSONUtil.parseObj(readJson(filePath));
String server = obj.getStr("server");
int port = obj.getInt("port");
String user = obj.getStr("username");
String password = obj.getStr("password");
String localBase = obj.getStr("localBase");
String saveBackupFile = obj.getStr("saveBackupFile");
String remoteDirectoryPath = obj.getStr("remoteDirectoryPath");
File dir1 = new File(localBase);
File[] dirs = dir1.listFiles();
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setBufferSize(1024000);
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
"OPTS UTF8", "ON"))) {
ftpClient.setControlEncoding("UTF-8");
}else {
ftpClient.setControlEncoding("GBK");
}
if (dirs!=null){
for (File dir : dirs) {
System.out.println(dir.getName());
if (dir.isDirectory()) {
File dir2 = new File(dir.getPath());
File[] files = dir2.listFiles();
String remoteDirectoryPathchange=remoteDirectoryPath+dir.getName();
ftpClient.changeWorkingDirectory(new String(remoteDirectoryPathchange.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
for (File file : files){
File localFile = new File(file.getPath());
FileInputStream inputStream = new FileInputStream(localFile);
String remoteFilePath = localFile.getName();
boolean uploaded = ftpClient.storeFile(new String(remoteFilePath.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), inputStream);
inputStream.close();
if (uploaded) {
String targetFolderPath=saveBackupFile+dir.getName();
moveFile(file.getPath(),targetFolderPath);
System.out.println("File uploaded successfully to: " + remoteFilePath);
} else {
System.out.println("Failed to upload file.");
}
}
}
}
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void moveFile(String sourceFilePath, String targetFolderPath) {
Path sourcePath = Paths.get(sourceFilePath);
Path targetPath = Paths.get(targetFolderPath);
if (!Files.exists(targetPath)) {
try {
Files.createDirectories(targetPath);
System.out.println(targetPath+"目标文件夹不存在,已创建。");
} catch (IOException e) {
System.err.println("无法创建目标文件夹: " + e.getMessage());
return;
}
}
try {
Files.move(sourcePath, targetPath.resolve(sourcePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件移动成功!");
} catch (IOException e) {
System.err.println("文件移动失败: " + e.getMessage());
}
}
public static JSONObject readJson(String jsonFilePath){
JSONObject jsonObject=new JSONObject();
try {
String jsonString = FileUtil.readString(jsonFilePath, Charset.defaultCharset());
jsonObject = JSONUtil.parseObj(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
}