Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FTP of a document to server using JAVA.

 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • 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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oooh, not familiar with the sun.net api's, I'll have to check them out.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic