我想做的是用java将一个目录从本地服务器上传到远程服务器。
为此,我使用了 com.jcraft.jsch 库。
我可以毫无问题地连接到远程服务器并将目录从本地计算机上传到远程服务器。
我创建了这个方法来做到这一点:
private void recursiveFolderUpload(String sourcePath, String destinationPath, ChannelSftp channelSftp)
throws SftpException, FileNotFoundException {
File sourceFile = new File(sourcePath);
if (sourceFile.isFile()) {
// copy if it is a file
channelSftp.cd(destinationPath);
if (!sourceFile.getName().startsWith("."))
channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);
} else {
System.out.println("inside else " + sourceFile.getName());
File[] files = sourceFile.listFiles();
if (files != null && !sourceFile.getName().startsWith(".")) {
channelSftp.cd(destinationPath);
SftpATTRS attrs = null;
// check if the directory is already existing
try {
attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName());
} catch (Exception e) {
System.out.println(destinationPath + "/" + sourceFile.getName() + " not found");
}
// else create a directory
if (attrs != null) {
System.out.println("Directory exists IsDir=" + attrs.isDir());
} else {
System.out.println("Creating dir " + sourceFile.getName());
channelSftp.mkdir(sourceFile.getName());
}
for (File f : files) {
recursiveFolderUpload(f.getAbsolutePath(), destinationPath + "/" + sourceFile.getName(),
channelSftp);
}
}
}
这没有问题,目录已转移,但有时我的目录中有快捷方式。
假设我有一个文件夹。其中我还有两个文件夹,其中一个是另一个文件夹的快捷方式。
执行该方法时,快捷方式的目录现在是它自己的文件,我不希望这样。
将目录上传到远程服务器时如何保留快捷方式?
谢谢
最佳答案
好吧,你必须编码。
检测本地文件实际上是符号链接(symbolic link): Detecting a symlink in Java .
并使用 ChannelSftp.symlink
在服务器上重新创建符号链接(symbolic link).
关于java - 将目录上传到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063195/