0

what I want to do is to upload a directory from a local server to a remote server in java.

To do that I used the com.jcraft.jsch library.

I can connect to my remote server and upload a directory from my local computer to my remote server without a problem.

I have created this method to do that:

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);
            }

        }
    }

This works with no problem, the directory is tranfered but sometimes I have shortcuts inside my directories.

Imagine that I have a folder.Inside it I have two more folders and one is a shortcut of the other for example.

When executing that method the directory that is a shortcut is now its own file and I dont want that.

How can I mantain the shortcut when upload the directory to my remote server?

Thanks

carlos
  • 63
  • 1
  • 1
  • 4

1 Answers1

0

Well, you have to code it.

Detect that the local file is actually a symlink: Detecting a symlink in Java.

And recreate the symlink on the server using ChannelSftp.symlink.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992