• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SFTP in Java

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"?
 
Brandon Booysen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi After uploading the file

How can i get some response saying that file uploaded successfully?
 
Brandon Booysen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

The ftp.upload(file args) - returns a void
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic