• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

FTP of a document to server using JAVA.

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me how to do FTP of a document to server using JAVA.
I can do get and put. But now i want to do dir listing and move through the directories of the remote machine.
Is there any documents available which will explain me about this.
Thanks
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to use the java.net.URL class. For FTP, you can use a string of ftp://<user>:<password>@<host>:<port>/<url-path>. After that, get a URLConnection. Look at those methods to see what you can do. You can get Streams, FileMappings, whole bunch of stuff.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jason,
actually i came across what you suggested earlier when i needed ftp client in java but i didn't figure out the way to upload the file (well even way to get the file)...
can you post a sample working code to us? though i already have a ftp client written in fully java...
regards
maulin.
 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A small smple code for getting file :
private void getFile() throws IOException
{
ftp = new FtpClient();
ftp.openServer("YOUR REMOTE SERVER");
ftp.login("USER ID","PASSWD");
ftp.ascii();
int c;
int len;
String nextLine = "";
try {
TelnetInputStream t = ftp.get("/u3/apps/estart_webapp/2003030715185XMLR00199999.XML");

byte buffer[] = new byte[9999];

FileOutputStream fos = new FileOutputStream( "C:/FTP/2003030715185XMLR001.XML" );
while( (len = t.read(buffer)) != -1 ) {
fos.write(buffer,0,len);
}
t.close();
fos.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("getting file INcomplete");
}
}
 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IM USING THE FOLLOWING PACKAGES
import sun.net.*;
import java.io.*;
import sun.net.ftp.*;

A sample for putting file :
public void sendFile() {
try
{
ftp = new FtpClient();
ftp.openServer("ny0sdev2.aig.com");
ftp.login("stradm","str@dm");
ftp.ascii();
writeFile("C:/XML/2003030615185XMLR001.XML","/u3/apps/estart_webapp/2003030615185XMLR001.XML");
ftp.closeServer();
}
catch(java.io.IOException ioe)
{
ioe.printStackTrace();
}

}
private void writeFile(String fileName, String destinationName) throws IOException
{
int c;
try {
TelnetOutputStream t = ftp.put(destinationName);
File file = new File(fileName);
if (file.exists() && file.canRead()) {
byte b[] = new byte[(int)file.length()];
FileInputStream fileInputStream = new FileInputStream(fileName);
fileInputStream.read(b);
fileInputStream.close();
t.write(b);
t.close();
}
System.out.println("closed output stream");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

BUT NOW I WANT TO GET THE FILE NAMES.. or the directory listing of the remote machine. Can any body help me doing this
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooh, not familiar with the sun.net api's, I'll have to check them out.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... personally I wouldn't use any sun.net classes, as they're not really intended for public release - just internal use. I'm not even sure where you find them now. Is there any public page where they are documented, or from which they can be downloaded? Or where you can find the source code?
I'd recommend using something like Jakarta Commons/Net. It's freely available, documented, and intended for public use.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic