Krishna
Krishna

Reputation: 3157

file getting corrupt while FTP

I am using com.enterprisedt.net.ftp jar to FTP files to remote location. My code looks like

try{
                    ftp.mkdir(actDirToSearch);      
                }catch(Exception e){
                    Log.addInLog(Log.ERR,e.getMessage());
                }           
                ftp.chdir(actDirToSearch);
                try{
                ftp.put(tarStream, fileName);
                }
                catch(Exception ex){
                    throw new FTPException(ex.getMessage());
                }

            }catch(FTPException e){
                    throw e;

            }catch(IOException e){
                throw e;

            }finally{
                try {
                    if(ftp != null){
                        ftp.quit();
                    }
               }

Also i am using this code to upload tar.gz file to 2 different remote machine having RHEL 5.4 and 6. But sometime i am getting successful message , sometime tar.gz file gets corrupt after uploading with lesser size on remote machine. While debugging i found the behavior that if i halt at if(ftp != null) line , and then after some time i execute ftp.quit() , it will always get succeed. I have seen through the ftp code , i found no separate thread to ftp the tar.gz file . Its all executing serially. My doubt is why this tar.gz file is getting corrupt , and why i am getting succeeded while debugging?

vsftpd services are running on both the machine. Also while doing the ftp manually from terminal , its getting succeeded. Java version is 1.6 .

Upvotes: 0

Views: 690

Answers (1)

arcy
arcy

Reputation: 13103

Check your FTP settings -- most FTP implementations have settings for the type of file; if it is text, binary, or whether the implementation is supposed to determine for itself what type it is.


in response to comment:

I am not familiar with the FTP protocol in great detail, but I know that FTP clients normally have a setting for "text" or "binary" files, and an option for the client to determine which kind of file is being transferred by looking at the first bytes in it. I expect this is something that is communicated by the client to the server, so that the server can do things like translate end-of-line to a set of bytes that is correct for the server's particular OS.

I gather you are not setting that, since you don't seem to know about it, and it could cause these symptoms. Read your library documentation and look for this. You refer to an 'ftp' object, look at the documentation (or the source) for that library and figure out if there isn't a way to set an option for text or binary.

You can also use a hex editor to look at the bytes in the source and the result files to see if you can see a pattern in the corruption -- do the bytes look all right until they get to a place where the source had an end-of-line character, but in fact it's a binary file? Is the server stripping off 8th bits (FTP goes back to the days of commonly used 7-bit ASCII, after all).

Upvotes: 1

Related Questions