Transferring file from local machine to server in WinSCP using Java

Ragesh Kr picture Ragesh Kr · May 28, 2014 · Viewed 8.6k times · Source

I want to write a notepad file and upload it to a WinSCP path.

I used to create those files manually: Open the WinSCP, give the credentials, open the desired path and upload the file.

Now I am in a situation to automate those things. Text will be entered in a text box or fetched from DB, it has to be written in a notepad file and should be uploaded to a WinSCP path. I have implemented writing the file and it is working fine.

Now, how do I upload the file using Java?

Answer

Martin Prikryl picture Martin Prikryl · May 28, 2014

What you call "WinSCP path" is actually a storage accessible via some file transfer protocol. Most typically a FTP or SFTP site.

For the FTP, you can use for example the URLConnection class:
Uploading to FTP using Java
or the Apache FTPClient client class:
FTPClient - Java, upload file

For the SFTP, you can use JSch Library:
How to retrieve a file from a server via SFTP?

If you really want to use WinSCP, you can interact with WinSCP scripting interface from Java. See a code sample in the following question:
How to know whether SFTP was successful or not in WinSCP