Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ftpclient ascii transfer mode not the same as ascii ftp from unix box

Posted on 2004-04-23
13
Medium Priority
?
717 Views
Last Modified: 2012-05-04
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(ftpaddress);
   System.out.println(ftpusername);
   System.out.println(ftppassword);
   System.out.println(ftpfilename);
   System.out.println(ftptransfermode);
   System.out.println(ftpremotefilename);
   System.out.println(ftpdirectory);

    FtpClient ftpClient = new FtpClient();
    ftpClient.openServer(ftpaddress);
    ftpClient.login(ftpusername, 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(ftpClient.put(ftpfilename));
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(ftpfilename));
    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
0
Comment
Question by:manishksinha-2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10903378
>>if(ftptransfermode=="A")

should strictly be

if ("A".equals(ftptransfermode))

Try that first
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 10903868
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 10905155
yeah  sun.net.ftp.FtpClient is pretty basic, wouldn't rely on it.
Try a better ftp implementation.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 10905197
how do the file sizes compare with the originals?
0
 

Author Comment

by:manishksinha-2004
ID: 11155168
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
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 252 total points
ID: 11155330
> Question is: How can I simulate this in java ftp program listed above.
Easy.  Just replace the CRs with CR/LF combinations.  As you are writing the file out, look for CR characters.  Instead of just writing out the CR, write out a CR/LF combination.

For the timeout part, I would put all the worker code that directly writes to and reads from the server into its own thread, waiting for requests from the rest of the program.  The rest of the program could poll it every couple of seconds to see if a response has come back.  Also create a watchdog thread that keeps track of how long it has been since the worker thread has done or received anything.  Every time the worker thread does or receives anything, it should reset the watchdog timer.  If the watchdog timer reaches 5 minutes without activity, it declares the worker timed out and acts to kill it and makes it clean up whatever resources it has allocated.

0
 
LVL 92

Expert Comment

by:objects
ID: 11156818
> How can I simulate this in java ftp program listed above.

Use BufferedReader to read file, and PrintWriter to save file to disk.
0
 

Author Comment

by:manishksinha-2004
ID: 11165451
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?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 248 total points
ID: 11166762
BufferedReader/PrintWriter should lhopefully ook after that for you.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question