aspose file tools*
The moose likes I/O and Streams and the fly likes FTP of a document to server  using JAVA. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "FTP of a document to server  using JAVA." Watch "FTP of a document to server  using JAVA." New topic
Author

FTP of a document to server using JAVA.

mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
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

Joined: May 08, 2001
Posts: 1932
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Sep 12, 2001
Posts: 81
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

Joined: Sep 12, 2001
Posts: 81
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

Joined: May 08, 2001
Posts: 1932
Oooh, not familiar with the sun.net api's, I'll have to check them out.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FTP of a document to server using JAVA.
 
Similar Threads
upload a file at server
Regarding FTP froma remote server
FTP in Java
How to ftp a file to a ftp server in java ??
how to connect FTP server in java