Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

org.apache.commons.net.ftp.FTPClient - help please

holodAug 21 2007 — edited Mar 11 2008
My code adds some bytes to file
I can't understand why!


Please, see small example...

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/*
 * FtpClientClassTest.java
 *
 * Created on 21 Август 2007 г., 9:49
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author SShpk
 */
public class FtpClientClassTest {
    
    /** Creates a new instance of FtpClientClassTest */
    public FtpClientClassTest() {
    }
    
    public static void main(String[] args){
        File file = new File("blablabla.jpg");
        String fileName = new String("newFileBugaga.jpg");
        String ftpServer = "192.168.36.4";
	String ftpUsername = "dev";
	String ftpPassword = "dev123";
	int ftpPort = 21;
 
	FTPClient ftpClient = new FTPClient();
 
	try {
		InetAddress inetAddress = InetAddress.getByName(ftpServer);
		ftpClient.connect(inetAddress, ftpPort);
 
		// After connection attempt, you should check the reply code to verify success.
		if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
			ftpClient.disconnect();
			throw new Exception("FTP server refused connection! Reply string from server is: "
					+ ftpClient.getReplyString());
		}
 
                ftpClient.enterLocalActiveMode();
 
		ftpClient.login(ftpUsername, ftpPassword);
                if(ftpClient.isConnected()){
                    System.out.println("logged in and connected ofcourse");
                }
 
		// Upload file 
                //Main problem
                //This code adds some bytes to file. 
                //I can't understand why!
                //It works with images (jpg,png e.t.c.) like with binary files and it's correct
		InputStream fis = new FileInputStream(file);
		OutputStream os = ftpClient.storeFileStream(fileName);
		
                //very stupid and slow code, but it appends some bytes...
                int t=0;
                while( (t=fis.read()) >=0){
                    os.write(t);
                }
                //byte buf[] = new byte[8192];
		//while (fis.read(buf) != -1) {
		//	os.write(buf);
		//}
		fis.close();
		os.close();
                System.out.println("streams are closed");
 
	} catch (Exception e) {
            try {
                throw new Exception("Error uploading to ftp server '" + ftpServer + "'!", e);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
	}  finally {
		if (ftpClient.isConnected()) {
			try {
				ftpClient.disconnect();
			} catch (IOException ioe) {
				//if (log.isWarnEnabled()) {
				//	log.warn("Failed to disconnect from the ftp server!", ioe);
				//}
			}
		}
	}
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2008
Added on Aug 21 2007
18 comments
6,217 views