0

Need a Java program to copy multiple files from local windows to Linux server and vice versa(upload and download) .I have written a utility which works for single file transfer. But when it comes for the multiple file while listing the files in a directory. The files are not getting searched out, due to the issue directory separator. In windows it is '\' where as Linux '/'. Is there any free library which helps to do so?

It should be using SCP protocol. here is the snippet makes the main issue for me:

String files;
System.out.println("files=");
System.out.println();
File folder = new File(path);
System.out.println("folder="+folder);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) 
{

    if (listOfFiles[i].isFile()) 
    {
        files = listOfFiles[i].getName();
        System.out.println("hello here are files");
        System.out.println(files);
    }
}
Irgendw Pointer
  • 1,770
  • 3
  • 30
  • 67
Rintos
  • 11
  • 1
  • 1
  • 3

1 Answers1

3

JSch might interest you. It's used for establishing a secure channel between two hosts - you can use it for SSH, SCP, SFTP, etc..

Example of establishing a connection: example

Example for SCP (directly from JCraft): example

This is how to get all properties(source and target absolute path from File object) for your files in a directory: http://pastebin.com/FXS9cHPW

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Thanks for the quick reply..the exmple shows for SFTP ...I need specifically SCP for multifile tranfer .Could you plz paste some example if have.... – Rintos Nov 29 '13 at 08:44
  • Of course. I edited in an example directly from the developers of JSch. Cheers! – Dropout Nov 29 '13 at 08:50
  • Yes That works for single files...I am facing the issue for multile files ..say when i give *.txt ,*.Jepg ,or simply * it shuld work..Can you plz help me to modify accordingly. – Rintos Nov 29 '13 at 08:58
  • Aren't you able to just do the same for all the files you need? Make a `List files` which will contain them and then `for(File thisFile : files){`.. I probably don't see the exact problem, sorry :( – Dropout Nov 29 '13 at 09:01
  • Yes there the problem lies...When I try to list files using the above snippet you have given it is not listing any files due to cross plateform compatibility problem.. – Rintos Nov 29 '13 at 09:09
  • There is no cross platform compatibility problem. Source file with absolute path is a String as well as target path. It doesn't matter to JSch/SCP if you set a target path `C:\stuff\ ` or `/home/you/stuff/` it gets resolved on the target system. The only problem is if you don't know WHERE to copy the file in the first place and you're inputing a bad target folder, then you need to implement some sort of searching mechanism, but traversing the target system like this is not a good idea. – Dropout Nov 29 '13 at 09:19
  • Or do you have a problem finding the files on your system? – Dropout Nov 29 '13 at 09:20
  • String files; System.out.println(); File folder = new File(rfile); System.out.println("folder="+folder); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { files = listOfFiles[i].getName(); System.out.println("hello here are files"); System.out.println(files); } } – Rintos Nov 29 '13 at 09:38
  • Here when i give local path like rfile=C:\\Users\\SCP Utility\\Source\\ it list out all files...but where as when i give rfile=/root/softwares/ it doest search and list out all file inside the folder software. (this is used when i tranfer file from remote linux machine to windows local machine ...)I might be confusing you:( – Rintos Nov 29 '13 at 09:41