Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:43:53+00:00 2026-05-30T01:43:53+00:00

Is it possible to do file/directory sync in Java using JSch ? I need

  • 0

Is it possible to do file/directory sync in Java using JSch ? I need to sync directory from a remote linux machine to my local windows machine. Is this possible ?

-Tivakar

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:43:54+00:00Added an answer on May 30, 2026 at 1:43 am

    The easiest way to download files from SCP server is using Commons VFS along with JSch:

    import java.io.*;
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.vfs2.*;
    
    public class CopyRemoteFile {
        public static void copyRemoteFiles(String host, String user, String remotePath, String localPath) throws IOException {
            FileSystemOptions fsOptions = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fsOptions, "no");
            SftpFileSystemConfigBuilder.getInstance().setIdentities(fsOptions,
                    new File[] { new File(FileUtils.getUserDirectoryPath() + "/.ssh/id_dsa") });
            DefaultFileSystemManager fsManager = (DefaultFileSystemManager) VFS.getManager();
            String uri = "sftp://" + user + "@" + host + "/" + remotePath;
    
            FileObject fo = fsManager.resolveFile(uri, fsOptions);
    
            FileObject[] files = fo.getChildren();
            for (FileObject file : files) {
                        // We will be dealing with the files here only
                if (file.getType() == FileType.FILE) {
                    FileUtils.copyInputStreamToFile(file.getContent().getInputStream(),
                            new File(localPath + "/" + file.getName().getBaseName()));
                }
                file.close();
            }
    
            fo.close();
    
            fsManager.close();
        }
    }
    

    It’s just an example I got in my Wiki, so nothing fancy. But do keep in mind that if you’ll close fsManager, you will not be able to open it again in the same VM. I got this issue while testing this solution…

    Although the example above does not import any JSch classes, you need to put it in the classpath anyway.

    The above example is using private key to authenticate with the remote host. You can easily change that by providing password and modifying the uri to include that.

    If you need to sync files, you can compare dates of the files on the local file system (or DB, or any other source of the information) and the remote files:

    import java.io.*;
    import org.apache.commons.io.*;
    import org.apache.commons.vfs2.*;
    import org.apache.commons.vfs2.impl.*;
    import org.apache.commons.vfs2.provider.sftp.*;
    
    public class CopyRemoteFile {
        public static void copyRemoteFiles(final String host, final String user, final String remotePath, final String localPath)
                throws IOException {
            FileSystemOptions fsOptions = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fsOptions, "no");
            SftpFileSystemConfigBuilder.getInstance().setIdentities(fsOptions,
                    new File[] { new File(FileUtils.getUserDirectoryPath() + "/.ssh/id_dsa") });
            DefaultFileSystemManager fsManager = (DefaultFileSystemManager) VFS.getManager();
            String uri = "sftp://" + user + "@" + host + "/" + remotePath;
    
            FileObject fo = fsManager.resolveFile(uri, fsOptions);
    
            FileObject[] files = fo.getChildren();
            for (FileObject file : files) {
                // We will be dealing with the files here only
                File newFile = new File(localPath + "/" + file.getName().getBaseName());
                if (file.getType() == FileType.FILE && newFile.lastModified() != file.getContent().getLastModifiedTime()) {
                    FileUtils.copyInputStreamToFile(file.getContent().getInputStream(), newFile);
                    newFile.setLastModified(file.getContent().getLastModifiedTime());
                } 
                file.close();
            }
    
            fo.close();
    
            fsManager.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to delete a file(s) inside the directory by just using javascript
Possible Duplicate: Not possible to launch a file on a network using Java Desktop?
I've been wondering: is it possible to shield a directory/file on a server from
Possible Duplicate: include file from different directory I have a site laid out as
I recently built a program that parses a remote file from \some_server\c$\directory\file.xls and it
Not sure if this is possible or not? I need contents of a file
My windows program receives information from another program via directory/file interface. That is the
Possible Duplicate: Build Tar file from directory in PHP without exec/passthru Does PHP have
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: Create Excel file in Java How to save output in excel format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.