manishksinha-2004
asked on
ftpclient ascii transfer mode not the same as ascii ftp from unix box
I am trying to use the ftpClient class to ftp a file in ascii mode to a windows machine.
But, the file size is different than when I use Hummingbird ftp or unix ftp.
I guess, other ftp command creates an additional character at the end of each line (carriage return or line feed) for windows systems. I think that the ftpClient does not do that. The file created by ftpClient is smaller than the ones created by humming bird or unix ftp.
Here is my sample java code:
import java.io.*;
import java.util.*;
import sun.net.ftp.FtpClient;
import sun.net.TelnetInputStream;
public class FtpUpload {
static String ftpaddress;
static String ftpusername;
static String ftppassword;
static String ftpfilename;
static String ftptransfermode;
static String ftpremotefilename;
static String ftpdirectory;
public static void main(String[] args) {
try {
ftpaddress = args[0];
ftpusername = args[1];
ftppassword = args[2];
ftpfilename = args[3];
ftptransfermode = args[4];
ftpremotefilename = args[5];
ftpdirectory = args[6];
System.out.println(ftpaddr ess);
System.out.println(ftpuser name);
System.out.println(ftppass word);
System.out.println(ftpfile name);
System.out.println(ftptran sfermode);
System.out.println(ftpremo tefilename );
System.out.println(ftpdire ctory);
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(ftpad dress);
ftpClient.login(ftpusernam e, ftppassword); // login
if(ftptransfermode=="A")
{
ftpClient.ascii(); // change to ascii mode transfer
}
else
{
ftpClient.binary(); // change to binary mode transfer
}
if(ftpdirectory!="")
{
ftpClient.cd(ftpdirectory) ; // change directory
}
BufferedOutputStream out = new BufferedOutputStream(ftpCl ient.put(f tpfilename ));
BufferedInputStream in = new BufferedInputStream(new FileInputStream(ftpfilenam e));
int b = -1;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
ftpClient.closeServer();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
Any suggestions??
Thanks
But, the file size is different than when I use Hummingbird ftp or unix ftp.
I guess, other ftp command creates an additional character at the end of each line (carriage return or line feed) for windows systems. I think that the ftpClient does not do that. The file created by ftpClient is smaller than the ones created by humming bird or unix ftp.
Here is my sample java code:
import java.io.*;
import java.util.*;
import sun.net.ftp.FtpClient;
import sun.net.TelnetInputStream;
public class FtpUpload {
static String ftpaddress;
static String ftpusername;
static String ftppassword;
static String ftpfilename;
static String ftptransfermode;
static String ftpremotefilename;
static String ftpdirectory;
public static void main(String[] args) {
try {
ftpaddress = args[0];
ftpusername = args[1];
ftppassword = args[2];
ftpfilename = args[3];
ftptransfermode = args[4];
ftpremotefilename = args[5];
ftpdirectory = args[6];
System.out.println(ftpaddr
System.out.println(ftpuser
System.out.println(ftppass
System.out.println(ftpfile
System.out.println(ftptran
System.out.println(ftpremo
System.out.println(ftpdire
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(ftpad
ftpClient.login(ftpusernam
if(ftptransfermode=="A")
{
ftpClient.ascii(); // change to ascii mode transfer
}
else
{
ftpClient.binary(); // change to binary mode transfer
}
if(ftpdirectory!="")
{
ftpClient.cd(ftpdirectory)
}
BufferedOutputStream out = new BufferedOutputStream(ftpCl
BufferedInputStream in = new BufferedInputStream(new FileInputStream(ftpfilenam
int b = -1;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
ftpClient.closeServer();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
Any suggestions??
Thanks
Unix systems use a single character (ASCII CR) to signal an End of Line, and windows systems use two characters (ASCII CR and LF) in their convention.
FTP servers are supposed to perform this translation when moving files between dos and unix machines in text mode (mode "A"), either adding or removing the LF characters as is required. If you instead use ftp's binary mode (mode "I") then the servers will keep their hands off the contents of the files, and not attempt to add or remove the LF characters.
FTP servers are supposed to perform this translation when moving files between dos and unix machines in text mode (mode "A"), either adding or removing the LF characters as is required. If you instead use ftp's binary mode (mode "I") then the servers will keep their hands off the contents of the files, and not attempt to add or remove the LF characters.
how do the file sizes compare with the originals?
ASKER
I am back again. Sorry for that. I have been trying out some perl scripts.
For my java test, I ftped a file from unix system to windows. The file on unix box contained 3 lines and had 45 bytes in it. Java ftp program created the same file with the same byte size, but hummingbird ftp program created the file on the windows box with a size of 48 bytes.
It seems to have added 3 bytes, 1 for each line (Line feed character).
Question is: How can I simulate this in java ftp program listed above.
Also, I wanted to know how to handle java ftp program when the remote server is not responding. Maybe, use threads... Sometime the java ftp program can hang if the remote server is not responding. I want to timeout after 5 minutes.
Thanks everyone
For my java test, I ftped a file from unix system to windows. The file on unix box contained 3 lines and had 45 bytes in it. Java ftp program created the same file with the same byte size, but hummingbird ftp program created the file on the windows box with a size of 48 bytes.
It seems to have added 3 bytes, 1 for each line (Line feed character).
Question is: How can I simulate this in java ftp program listed above.
Also, I wanted to know how to handle java ftp program when the remote server is not responding. Maybe, use threads... Sometime the java ftp program can hang if the remote server is not responding. I want to timeout after 5 minutes.
Thanks everyone
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> How can I simulate this in java ftp program listed above.
Use BufferedReader to read file, and PrintWriter to save file to disk.
Use BufferedReader to read file, and PrintWriter to save file to disk.
ASKER
Do you have a test code for the threads?
I guess the CR/LF is only for windows system, and not for unix. How can one find out what system is the file being written to?
I guess the CR/LF is only for windows system, and not for unix. How can one find out what system is the file being written to?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
should strictly be
if ("A".equals(ftptransfermod
Try that first