java将目录上载到远程服务器
我想做的是用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);
}
}
}
这没有问题,目录是被传输的,但有时我在目录中有快捷方式
想象一下,我有一个文件夹。里面还有两个文件夹,一个是另一个的快捷方式
当执行该方法时,作为快捷方式的目录现在是它自己的文件,我不希望这样
将目录上载到远程服务器时,我如何保持快捷方式
谢谢
# 1 楼答案
好吧,你得给它编码
检测本地文件实际上是一个符号链接: Detecting a symlink in Java
并使用^{} 在服务器上重新创建符号链接