Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: FTP: transfer works fine in windows but not in linux machine

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default FTP: transfer works fine in windows but not in linux machine

    Hi all,

    I'm new and i'm faced to an issue when i transfer files from my ftp server(windows) to my local drive (linux).
    What I want to do is: I need to loop on all directories in my server and copy if there is a file.
    I want to precise that my ftp server is on a windows machine and when I test the code on a windows machine it's work fine, however when i launch it on a linux machine it's does'nt run and doesn't give any error. It's just still loading
    Can anyone help me. Thanks in advance.This is the code:

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
     
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
     
     
    public class FTPManager {
     :( 	
    	static Locale locale = Locale.getDefault();
    	static Date actuelle = new Date();
    	static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
     
    	public FTPManager(){
    	}
     
    	private FTPClient getConnector(FTPClient ftpClient,String server,int port,String user,String pwd){
    	    try {
    	    	ftpClient.connect(server, port);
    	    	ftpClient.login(user, pwd);
    	    } catch (IOException ex) {
    	        ex.printStackTrace();
    	    }	
    	    return ftpClient;
    	}
     
    	 public static void scanFolder(String server,int port,String user,String pwd){
    		 	FTPClient ftpClient = new FTPClient();
    		 	FTPManager ftpManager = new FTPManager();
    		 	ftpManager.getConnector(ftpClient, server, port, user, pwd);
    	        FTPFile[] ftpFiles;
    			try {
    				ftpFiles = ftpClient.listFiles();
    		        for (FTPFile ftpFile : ftpFiles)
    		        {
    		             if (ftpFile.isDirectory())
    		            {
    		                String dirName=ftpFile.getName();
    		                try {
     
    		                	ftpManager.scanDirectoryTree(ftpClient, dirName);
     
    						} catch (IOException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						} catch (Exception e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    		            }
    		        }
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			try {
    				ftpClient.disconnect();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	 }
    	    private void scanDirectoryTree(FTPClient ftpClient,String directory) throws Exception
    	    {   
     
    	       FTPFile[] ftpFiles =ftpClient.listFiles(directory);
    	       String dat = dateFormat.format(actuelle);
    	        for (FTPFile ftpFile : ftpFiles)
    	        {
    	            if (ftpFile.isFile())
    	            {
     
    	            	String fileName = ftpFile.getName();
    	            	System.out.println("FILE : "+fileName);
     
    	                if(fileName.contains("Fact")){
    	                	ftpClient.enterLocalPassiveMode();
    	                    ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
    	                    String remoteFile1 = directory+"\\" + fileName;
    	                    File downloadFile = new File("D:/Test/Fact/"+fileName);
    	                    File downloadFileArchive = new File("D:/Test/Archive/"+fileName+"_"+dat);
    	                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
    	                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFileArchive));
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream)){
    	                    	System.out.println("FileOut Facture : " + outputStream.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream1)){
    	                    	System.out.println("FileOut 1 Facture : " + outputStream1.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    //ftpClient.deleteFile(remoteFile1);
    	                    outputStream.close();
    	                    outputStream1.close();
     
    	                }
    	                else if(fileName.contains("Ref")){
    		                ftpClient.enterLocalPassiveMode();
    	                    ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
    	                    String remoteFile1 = directory+"\\" + fileName;
    	                    File downloadFile = new File("D:/Test/Ref/"+fileName);
    	                    File downloadFileArchive = new File("D:/Test/Archive/"+fileName+"_"+dat);
    	                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
    	                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFileArchive));
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream)){
    	                    	System.out.println("FileOut reference : " + outputStream.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream1)){
    	                    	System.out.println("FileOut 1 reference : " + outputStream1.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    //ftpClient.deleteFile(remoteFile1);
    	                    outputStream.close();
    	                    outputStream1.close();  	
    	                }
    	            }
    	        }
    	        for (FTPFile ftpFile : ftpFiles)
    	        {
    	            if (ftpFile.isDirectory())
    	            {
    	                String dirName=directory+"\\"+ftpFile.getName();
    	                System.out.println("DIRECTORY : "+dirName);
    	                scanDirectoryTree(ftpClient, dirName);
     
    	            }
    	        }
     
    	    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: FTP: transfer works fine in windows but not in linux machine

    on a linux machine it's does'nt run and doesn't give any error
    Try debugging the code by adding some println statements to show where the code is executing and what the values of the variables are. The print out should give you some clues about what the problems could be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    nssarah (May 10th, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FTP: transfer works fine in windows but not in linux machine

    Thank you to reply me.
    I've already put many println and this is what happened. On the root directory there are 2 folders and ih those folders there are subdirectories, the code is able to get only the files in the first subdirectories and it's locked here.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: FTP: transfer works fine in windows but not in linux machine

    I don't have a linux system for testing. Someone with a linux system may be able to help you, but I think they will like to have a small, complete program that compiles, executes and shows the problem for testing. If the code is having a problem with accessing files in subdirectories, then that is what should be in the testing code without any network code.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FTP: transfer works fine in windows but not in linux machine

    Hi,

    This is the code with the println :

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
     
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
     
     
    public class FTPManager {
     
    	static Locale locale = Locale.getDefault();
    	static Date actuelle = new Date();
    	static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
     
    	public FTPManager(){
    	}
     
    	private FTPClient getConnector(FTPClient ftpClient,String server,int port,String user,String pwd){
    	    try {
    	    	ftpClient.connect(server, port);
    	    	ftpClient.login(user, pwd);
    	    } catch (IOException ex) {
    	        ex.printStackTrace();
    	    }	
    	    return ftpClient;
    	}
     
    	 public static void scanFolder(String server,int port,String user,String pwd){
    		 	FTPClient ftpClient = new FTPClient();
    		 	FTPManager ftpManager = new FTPManager();
    		 	ftpManager.getConnector(ftpClient, server, port, user, pwd);
    	        FTPFile[] ftpFiles;
    			try {
    				ftpFiles = ftpClient.listFiles();
    		        for (FTPFile ftpFile : ftpFiles)
    		        {
    		             if (ftpFile.isDirectory())
    		            {
    		                String dirName=ftpFile.getName();
    		                try {
     
    		                	ftpManager.scanDirectoryTree(ftpClient, dirName);
     
    						} catch (IOException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						} catch (Exception e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    		            }
    		        }
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			try {
    				ftpClient.logout();
    				ftpClient.disconnect();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	 }
    	    private void scanDirectoryTree(FTPClient ftpClient,String directory) throws Exception
    	    {   
     
    	       FTPFile[] ftpFiles =ftpClient.listFiles(directory);
    	       String dat = dateFormat.format(actuelle);
    	        for (FTPFile ftpFile : ftpFiles)
    	        {
    	            if (ftpFile.isFile())
    	            {
     
    	            	String fileName = ftpFile.getName();
    	            	System.out.println("FILE : "+fileName);
     
    	                if(fileName.contains("Fact")){
    	                	System.out.println("FACT");
    	                	ftpClient.enterLocalPassiveMode();
    	                    ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
    	                    String remoteFile1 = directory+"\\" + fileName;
                               /*
    	                     * For LINUX Client :
                                 * File downloadFile = new File("/tmp/Fact/"+fileName);
                                 * File downloadFileArchive = new File("/tmp/Archive/"+fileName+"_"+dat);
    	                     */
    	                    File downloadFile = new File("D:/Test/Fact/"+fileName);
    	                    File downloadFileArchive = new File("D:/Test/Archive/"+fileName+"_"+dat);
    	                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
    	                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFileArchive));
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream)){
    	                    	System.out.println("FileOut Fact : " + outputStream.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream1)){
    	                    	System.out.println("FileOut 1 Fact : " + outputStream1.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    //ftpClient.deleteFile(remoteFile1);
    	                    outputStream.close();
    	                    outputStream1.close();
     
    	                }
    	                else if(fileName.contains("Ref")){
    	                	System.out.println("REF");
    		                ftpClient.enterLocalPassiveMode();
    	                    ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
    	                    String remoteFile1 = directory+"\\" + fileName;
                                /*
    	                     * For LINUX Client :
                                 * File downloadFile = new File("/tmp/Fact/"+fileName);
                                 * File downloadFileArchive = new File("/tmp/Archive/"+fileName+"_"+dat);
    	                     */
    	                    File downloadFile = new File("D:/Test/Ref/"+fileName);
    	                    File downloadFileArchive = new File("D:/Test/Archive/"+fileName+"_"+dat);
    	                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
    	                    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFileArchive));
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream)){
    	                    	System.out.println("FileOut ref : " + outputStream.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    if (ftpClient.retrieveFile(remoteFile1, outputStream1)){
    	                    	System.out.println("FileOut 1 reference : " + outputStream1.toString());
    	                    }
    	                    else{
    	                    	throw new Exception("Failed to download file completely: " + remoteFile1);
    	                    }
    	                    //ftpClient.deleteFile(remoteFile1);
    	                    outputStream.close();
    	                    outputStream1.close();  	
    	                }
    	            }
    	        }
    	        for (FTPFile ftpFile : ftpFiles)
    	        {
    	            if (ftpFile.isDirectory())
    	            {
    	                String dirName=directory+"\\"+ftpFile.getName();
    	                System.out.println("DIRECTORY : "+dirName);
    	                scanDirectoryTree(ftpClient, dirName);
     
    	            }
    	        }
     
    	    }
    }

    This is what i've got when i call the scanFolder() method in a main class.

    public class Main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		FTPManager.scanFolder(server,port,user,pwd);
    	}
    }

    **********On windows**********
    DIRECTORY : Directory1\SubDirectory1
    FILE : fileFact.txt
    FACT
    FileOut Fact : java.io.BufferedOutputStream@1a1bc40
    FileOut 1 Fact : java.io.BufferedOutputStream@14e40da
    DIRECTORY : Directory1\SubDirectory2
    FILE : fileRef.txt
    REF
    FileOut ref : java.io.BufferedOutputStream@83b00f
    FileOut 1 ref : java.io.BufferedOutputStream@19bd735

    DIRECTORY : Directory2\SubDirectory1
    FILE : fileFact.txt
    FACT
    FileOut Fact : java.io.BufferedOutputStream@4e17f9
    FileOut 1 Fact : java.io.BufferedOutputStream@107f742
    DIRECTORY : Directory2\SubDirectory2
    FILE : fileRef.txt
    REF
    FileOut ref : java.io.BufferedOutputStream@1fa490e
    FileOut 1 ref : java.io.BufferedOutputStream@d23e53


    **********On linux**********
    Linux.JPG


    I hope that it is more clear like this
    Thank you for your help!!

  7. #6
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: FTP: transfer works fine in windows but not in linux machine

    I received a time out if I didn't stop the execution:

    java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
    at javax.net.DefaultSocketFactory.createSocket(Socket Factory.java:206)
    at org.apache.commons.net.ftp.FTPClient._openDataConn ection_(FTPClient.java:502)
    at org.apache.commons.net.ftp.FTPClient.retrieveFile( FTPClient.java:1276)
    at FTPManager.scanDirectoryTree(FTPManager.java:104)
    at FTPManager.scanDirectoryTree(FTPManager.java:156)
    at FTPManager.scanFolder(FTPManager.java:53)
    at Main.main(Main.java:8)
    org.apache.commons.net.ftp.FTPConnectionClosedExce ption: FTP response 421 received. Server closed connection.
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java :346)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.jav a:490)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.jav a:534)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.jav a:583)
    at org.apache.commons.net.ftp.FTP.pasv(FTP.java:882)
    at org.apache.commons.net.ftp.FTPClient._openDataConn ection_(FTPClient.java:497)
    at org.apache.commons.net.ftp.FTPClient.initiateListP arsing(FTPClient.java:2296)
    at org.apache.commons.net.ftp.FTPClient.initiateListP arsing(FTPClient.java:2269)
    at org.apache.commons.net.ftp.FTPClient.listFiles(FTP Client.java:2046)
    at FTPManager.scanDirectoryTree(FTPManager.java:79)
    at FTPManager.scanFolder(FTPManager.java:53)
    at Main.main(Main.java:8)
    java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutp utStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStre am.java:136)
    at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder. java:202)
    at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEnc oder.java:272)
    at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.j ava:276)
    at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java: 122)
    at java.io.OutputStreamWriter.flush(OutputStreamWrite r.java:212)
    at java.io.BufferedWriter.flush(BufferedWriter.java:2 36)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.jav a:472)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.jav a:534)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.jav a:583)
    at org.apache.commons.net.ftp.FTP.quit(FTP.java:794)
    at org.apache.commons.net.ftp.FTPClient.logout(FTPCli ent.java:697)
    at FTPManager.scanFolder(FTPManager.java:69)
    at Main.main(Main.java:8)

Similar Threads

  1. connect to linux server from windows
    By ridg18 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 17th, 2013, 05:35 AM
  2. Java application works fine in IDE but not in windows
    By japonce in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 20th, 2012, 09:14 PM
  3. LDAP Sort 2 attributes returns NO results. Works fine if sorted on one.
    By funnyguy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 30th, 2011, 01:31 AM
  4. Program works fine but with friend it doesn't work
    By Rizza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2011, 01:20 PM
  5. jar file works on XP but not on Linux
    By cl2606 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 10th, 2011, 09:19 AM