File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes SFTP in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "SFTP in Java" Watch "SFTP in Java" New topic
Author

SFTP in Java

Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi

I am making a connection to the Server but want to transfer a file to it. But currently my code work the other way around. It's taking the file from the server and putting the file in the local directory.

Also i would like to add a response back from the server when the file is completed.

Below is my code

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 7921

Please UseCodeTags. I've fixed your post for you. It is also polite to PostRealCode. If you post code that doesn't compile, it looks like you aren't Showing Effort.
For example, if you want to upload a file, why would you invoke a method called "download"?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi Joe

Thank for information me about the Code Tags. I am very new to Java and the site.

The code works file. I can see the file downloading from the FTP site and putting it on the c drive. But I want it to work the other way around. I have tried to alter the code but I am getting an Error saying that the file does not exist. I would like to think that it searching for the file in the FTP directory which is incorrect. The file should go search in the c drive and past it on the FTP directory
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 7921

Brandon Booysen wrote:
The code works file.


The code you posted will not compile. Look at line 45.

Brandon Booysen wrote:
I can see the file downloading from the FTP site and putting it on the c drive. But I want it to work the other way around. I have tried to alter the code but I am getting an Error saying that the file does not exist. I would like to think that it searching for the file in the FTP directory which is incorrect. The file should go search in the c drive and past it on the FTP directory


I'm not sure what you mean when you say "search". That implies that there's some sort of logic the code will use to locate a file. As far as I can tell, you code will look in C:/tmp for a file named "TestUpload.txt". No "search" takes place.
Does the file exist on the FTP server (since you are downloading it first)?
Will the FTP process reliably complete before you try to upload?
What will happen if the file exists on the server? Does the upload fail?
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi

That was a typing Error. Here is the method


I'm not sure what you mean when you say "search". That implies that there's some sort of logic the code will use to locate a file. As far as I can tell, you code will look in C:/tmp for a file named "TestUpload.txt". No "search" takes place.
Reply: Well i am trying to pick up the file(TestUpload.txt) from my local directory

Does the file exist on the FTP server (since you are downloading it first)
Reply: Yes the file does exist on the FTP site

Will the FTP process reliably complete before you try to upload?
Reply:Well I am trying to build it and add a few steps to check of there file is in the pick up directory and add a few other checks(not sure if this answers the question)


What will happen if the file exists on the server? Does the upload fail?
Reply: Well this will form part of my checks first to check if the file exist if not send a mail to a group and if the file uploaded completed get the reply status from the FTP server and use that to send a other file.
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi

It's working I have changed
ftp.download("TestUpload.txt");
to
ftp.upload("TestUpload.txt");

And it's now uploading the file to the FTP server. Now if only need to get some message back from the FTP server.
How do i get that message from the FTP server saying the file completed successfully?
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi After uploading the file

How can i get some response saying that file uploaded successfully?
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi I added the following to my code

InputStream fis = null;
byte buf[] = new byte[8192];
System.out.print(buf);

and get a [B@186db541199 reply

I am not sure what does this mean or does is mean anything
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Brandon Booysen wrote:Hi I added the following to my code

InputStream fis = null;
byte buf[] = new byte[8192];
System.out.print(buf);

and get a [B@186db541199 reply

I am not sure what does this mean or does is mean anything

buf[] is array of bytes. A byte is 8 bits - not a character. If you want to print the contents of buf[], you need to convert the contents of buf[] to characters or the whole array to a String. System.out.print(buf); is attempting to use buf.toString(), which is not overridden to display the contents of the array. The default implementation of toString() prints out information about the array instance.
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi Tom

I am not sure how to do this. But when the upload completes and just need to retrieve a msg or response?
Could you please assist me with this?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I did a quick Google search and found this in the Sun documentation. Hope it helps... http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html
Brandon Booysen
Greenhorn

Joined: Jun 09, 2010
Posts: 24
Hi

The ftp.upload(file args) - returns a void
Deepa
Greenhorn

Joined: Jul 14, 2008
Posts: 1
hi All,


Is there a way to get return code from the server if the file is successfully transferred ? I'm using JSCAPE, and i see that upload method is void. Please help. Thanks.
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18365

My guess is that if the file is not successfully transferred an exception will be thrown. If this is the case, the absence of any exceptions indicates a successful transfer.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
 
subject: SFTP in Java
 
Threads others viewed
File transfering from client machine to Server through FTP.
File transfering from client machine to Server through FTP.
Password encripting
Trying to upload a file through jscape sftp API in java and not able to set the default directory
Connect to FTP site with Apache commons net FTP client through Proxy
IntelliJ Java IDE