Question

File does not transfer from UNIX to Windows using Apache Commons Net 2.0 FTPClient

Asked by: infotechCDI

I am working on creating a program which is meant to run on our HP-UX system.  The purpose of having it run on UNIX is to help with automating our process.  There is an app that runs on UNIX which generates a file, and I created a program which takes this file, converts to XML, and ftp's it to our Windows FTP Server (where it is picked up by a vendor).

The problem I am running into is that even though my logs show 'file transfer successful', the file is never uploaded to the FTP server.  Code below is from a test program I created just to work on the file transfer specifically. I've tried both the storeFile & remoteStore method.  When I use storeFile, it actually uploads an empty file named 123.txt.  When I use remoteStore nothing happens, but it does claim the file was transferred successfully.

I'm sure it's something simple like a configuration but I've tried several different things so far - setting transfer type to Binary, using FTPClientConfig to specify a UNIX system, etc. - with no luck. Any help is appreciated.

Thanks in advance.

import java.io.IOException;
import org.apache.commons.net.ftp.*;
import java.io.File;
 
public class TestFtp {
	private static FTPClient ftp;	
 
	public static void main(String[] args) {
		try	{
		  ftp = new FTPClient();
		  ftp.connect("ftp.mydomain.com");
		    
		  int reply = ftp.getReplyCode();
		  if(!FTPReply.isPositiveCompletion(reply)) {
		    ftp.disconnect();
		    System.out.println("FTP server refused connection.");
		    System.exit(1);
		  } else {
		    if (!ftp.login("denisem", "password")){
	            ftp.logout();
	            throw new FTPConnectionClosedException();
	          }
		  System.out.println("Successfully logged into CDI ftp.");
		  ftp.enterRemotePassiveMode();
		  
		  File file = new File("/home/denisem/123.txt");
		  if (file.exists()){
                    //FileInputStream fis = new FileInputStream("/home/denisem/123.txt");
		    //ftp.storeFile("123.txt", fis);
		    //fis.close();
		    ftp.remoteStore("/home/denisem/123.txt");
		    System.out.println("File successfully transferred");
		 } else
		    System.out.println("Cannot find the file on the local system");
               }
	       ftp.logout();
	       }catch(FTPConnectionClosedException ex){
		  System.out.println("The connection to the CDI has been closed unexpectedly: " + ex.getMessage());
                  System.exit(1);
		}catch(IOException ex){
		   System.out.println("A problem occured during file transfer with CDI ftp: " + ex.getMessage());
                   System.exit(1);
 	       } finally {
                   if(ftp.isConnected()) {
	           try {
	             ftp.disconnect();
	           } catch(IOException ioe) {} 
	      }
 	    }		
   }
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-30 at 08:02:34ID24858269
Tags

Java

,

Commons Net 2.0

Topics

Java Programming Language

,

HP-UX Unix

,

File Transfer Protocol (FTP)

Participating Experts
3
Points
500
Comments
27

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Truss for HP ux
    Is there an equivalent to the Solaris command truss in HP UX 11.x?
  2. GCC on HP/UX 11 - 64bit
    Does GCC work OK on HP/UX rev 11.0 - 64bit version? I have installed the version form the HP porting centre. 2.95.2 It seems to compile without warning messages but some programs do not work correctly. sizeof(long) and sizeof(size_t) are both reported as being 4. How come? Wh...
  3. HP-UX 10.20: GNU Make binary required
    Dear Folks, I have a HP-UX 10.20 system. I need to get hold of GNU make binary for my system. I cannot compiel GNU make because I do not have gcc. I would like a statically linked binary. Thanks
  4. HP-UX 10.20 and PHP question
    hi i am trying to setup php on my hp-ux 10.20 machine. I found depot software for apache and mysql. All i need is to install php and to configure the httpd.conf file. I found an unix verions of php. When i run the configure file however i get the error ^M not found for abou...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: afibarraPosted on 2009-10-30 at 08:24:53ID: 25703886

For simple FTP try "edtFTP" free library:

http://www.enterprisedt.com/products/edtftpj/download.html

It´s very simple and intuitive to use.

Hope this helps

 

by: CEHJPosted on 2009-10-30 at 08:52:56ID: 25704165

Use storeFile and check its return value

 

by: CEHJPosted on 2009-10-30 at 08:58:39ID: 25704241

Try the following, bearing in mind that you might need to CD remotely if you're not in the right dir

FileInputStreamfis = new FileInputStream(file);
boolean success = ftp.storeFile("123.txt",fis);
fis.close();
if (!success) {
	// Report error
}
                                              
1:
2:
3:
4:
5:
6:

Select allOpen in new window

 

by: infotechCDIPosted on 2009-10-30 at 09:26:06ID: 25704531

afibarra, thanks for the suggesting, but I have an entire app built already using Commons Net 2.0 - would prefer not to go back and have to recreate anything.

CEHJ - thanks for the tip.  It did help, but the file still will not transfer.  I tried throwing exceptions if success was false, but there does not seem to be any exception occuring because when I try to get the exception message it says null.

Of course this program runs fine when I run it from my Windows PC, it's only when run on UNIX do I have the problem. I had our network admin check the firewall since our UNIX system is on a different vlan but he sees no denied traffic, so I'm not quite sure what else it could be.

Thanks.

 

by: infotechCDIPosted on 2009-10-30 at 09:27:22ID: 25704548

CHEJ, one more note - I am executing the jar file from my home dir on UNIX which is where 123.txt is (even though I specify the full path in the program), and when I log into the ftp server using my credentials I am in the directory I am trying to upload to - so I'm not sure any changing of directories is necessary.  Thanks.

 

by: CEHJPosted on 2009-10-30 at 09:29:45ID: 25704574

>>I tried throwing exceptions if success was false, but there does not seem to be any exception occuring because when I try to get the exception message it says null.

Please post the code you used. If you're able to try to get the message, that would mean of course that it *did* throw an exception

 

by: infotechCDIPosted on 2009-10-30 at 09:31:45ID: 25704592

I told it to throw the exception in hopes to get a message... I'm definitely not "new to Java", but I am by no means an expert so this may not be the best way to go about it... open to any suggestions.  Thanks.

File file = new File("/home/denisem/123.txt");
if (file.exists()){
  FileInputStream fis = new FileInputStream(file);
  boolean success = ftp.storeFile("123.txt", fis);	
  fis.close();					
  if (success)
    System.out.println("File successfully transferred");
  else
     throw new Exception();
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: CEHJPosted on 2009-10-30 at 09:34:55ID: 25704626

You're not getting a message since you're not supplying one to the Exception ctor:

>>throw new Exception();

Try

throw new RuntimeException("Did not transfer file");

 

by: infotechCDIPosted on 2009-10-30 at 09:40:34ID: 25704684

Sorry, it was a sloppy mistake.  I was expecting something more along the lines of a printStackTrace() output.

In any event, you have helped me point out that the file transfer is in fact failing... however, I am desperate to figure out why it is failing and how to get it to work.

Thanks so much!

 

by: CEHJPosted on 2009-10-30 at 09:45:14ID: 25704725

>>I was expecting something more along the lines of a printStackTrace() output.

You can of course call printStackTrace if you want, but in this case, it's quite obvious what the origin is - IF there's a message ;-)

It's failing probably for the reason i gave: you need to CD (unless at least you can write to the 'root' of the server)

 

by: infotechCDIPosted on 2009-10-30 at 10:13:07ID: 25704988

CEHJ, my apologies - I didn't think that was the solution since I am specifying the full file path to the file on UNIX, and since when I connect to the FTP server with my credentials I am in the remote directory that I need to be in and that I have full access to.  If the problem was a change directory issue I would assume that it would also fail when I attempt to run this from my windows machine (specifying the windows path to the file on my desktop).

I will play with changing the directories though to see if that fixes the issue.  Thanks.

 

by: CEHJPosted on 2009-10-30 at 10:20:59ID: 25705058

No, i take your point. What's the output of the following?

ls -l /home/denisem/123.txt

 

by: infotechCDIPosted on 2009-10-30 at 10:41:47ID: 25705234

Here is the output:
/home/denisem> ls -l /home/denisem/123.txt
-rw-r--r--   1 denisem    tipixgrp        67 Oct 30 13:32 /home/denisem/123.txt

I did some more tests... the problem is specifically only with the sending of a file.
1.  Connect to windows ftp server from UNIX and delete remote file - success
2.  Connect to windows ftp server and download remote file to UNIX (using FileOutputStream) - 123.txt gets created in /home/denisem, but no data is transfered so file is 0byte
3.  Connect to windows ftp server and upload file to remote server - fail

I used the same 123.txt in the same locations for all cases just to rule out the file or paths being the problem.  This seems to point either a configuration issue in the ftp program when ftp'ing between unix & windows - or a network issue.  I also checked the FTP server logs - nothing looked out of the ordinary except this line:
[2] Fri 30Oct09 13:36:11 - (003551) REST 0
[6] Fri 30Oct09 13:36:11 - (003551) 350 Restarting at 0. Send STORE or RETRIEVE.

Could it be possible that UNIX is not actually sending these commands?  But if that were the case I would think it would not work when I manually ftp from UNIX to Windows.  This is very strange indeed.  Ultimately I can just create the application to run on a client machine and act as an intermediary but it would be ideal to have my app run directly on the UNIX system.  Just not sure where to go from here.

Thanks for your help.

 

by: CEHJPosted on 2009-10-30 at 10:49:14ID: 25705291

Shall have to think about this. What you could do for now if you can manage it is to try to put your 'manual' ftp session into debug and then mimic that in your program

 

by: objectsPosted on 2009-10-30 at 15:06:09ID: 25707110

> Sorry, it was a sloppy mistake.  I was expecting something more along the lines of a printStackTrace() output.

Its not a mistake. You would get that if an exception was thrown, there is no need to specify a message in the exception

and from what you have said you don't need to change directories

suggest you try a different ftp client as afibarra originally suggested.
Also worth testing on a different ftp server

Will save you wasting time changing things that aren't busted.

 

by: objectsPosted on 2009-10-30 at 15:08:18ID: 25707117

alse check you don't have any firewalls involved between the two

 

by: infotechCDIPosted on 2009-11-02 at 06:46:06ID: 25719759

objects, thanks for your input. There is a firewall - but our network admin confirmed it is not blocking the ftp traffic.  I can't seem to win on UNIX - this is actually the third FTP client that I've tried and found that it worked so well when running from my Windows machine so the entire application is already built.  We were going to have this app run from the windows machine but after some consideration thought it would benefit us to have it run directly from UNIX... until we ran into this road block.  It just doesn't seem to make much sense - but the fact that I can perform the transfer when executing it manually tells me there is something with this FTP API that UNIX doesn't like...

 

by: CEHJPosted on 2009-11-02 at 07:05:38ID: 25719976

Did you debug FTP yet?

 

by: infotechCDIPosted on 2009-11-02 at 07:32:48ID: 25720257

CEHJ, going to be working on this today.  I will post updates as soon as I can.  Thanks!

 

by: CEHJPosted on 2009-11-02 at 08:41:01ID: 25720936

Try running the app as root - see if that makes any difference

 

by: infotechCDIPosted on 2009-11-02 at 11:43:27ID: 25722914

No luck running it as root.  Having some difficulties debugging too.  Trying to research bugs to see if this is a known issue.  There has to be something happening only because it is UNIX since the same exact code works fine when running from my Windows PC.  I may have to consider leaving the FTP out of my application entirely and having our UNIX programmer script the file transfer separately.  Either that or I have to leave this as is to be run manually on a windows system.  Still trying though...

Thanks.

 

by: CEHJPosted on 2009-11-02 at 11:54:12ID: 25723042

Is there another FTP server you could test your code against?

 

by: infotechCDIPosted on 2009-11-02 at 11:58:27ID: 25723082

I absolutely DO NOT believe this!!!  Resolution = ftp.enterLocalPassiveMode(); !!!

I had ftp.enterRemotePassiveMode(); in my code - this was because the connection was not completing and our network admin suggested I connect to our ftp server using Passive Mode.  I made the assumption that since I was connecting to a Remote System, and I wanted to use Passive Mode, this was the method I should use.  The API documentation says to use this method for server to server transfer... I made the assumption that because the HP-UX system was a server (and is in fact acting as an ftp server for other purposes) that this was the method to use.

While poking around through the known bugs, someone had suggested using enterLocalPassiveMode() to deal with a 'hanging' issue, so I figured why the heck not try it.  I cannot believe that it was something so simple!  I am so sorry to have wasted anyone's time.  At the very least I hope this post helps someone else in the future who runs into the same issue.

Is there any way to assign points without accepting as a solution?  CHEJ I would like to award points for all of your help (sorry everyone else, but CHEJ is the only one who didn't suggest I use a different ftp client) - but I don't want to mess up the web sites problem/solution stats.

Thanks again to everyone, and again my apologies.

 

by: CEHJPosted on 2009-11-02 at 13:13:12ID: 25723791

infotechCDI, i'm glad you've got it sorted out. Don't worry about the points - i have quite a few already :-)

 

by: objectsPosted on 2009-11-02 at 13:16:02ID: 25723817

> but CHEJ is the only one who didn't suggest I use a different ftp client)

Where did I suggest you use a different client?

 

by: infotechCDIPosted on 2009-11-02 at 13:21:03ID: 25723871

>suggest you try a different ftp client as afibarra originally suggested.

Sorry, I didn't mean to offend anyone.  This was just a test program, but as I responded to afibarra, I have an entire app developed & tested (on Windows) using Commons Net 2.0 and I preferred to get the test working with this client so I wouldn't have to change my entire app.  The actual app uses the FTP for many other functions (not simply a single file transfer) so to me it was worth the time trying to get this client to work if I possibly could.  I do appreciate everyone's feedback though!

 

by: infotechCDIPosted on 2009-11-02 at 13:25:10ID: 25723914

I would like to reassign some of the points to user 'objects' as this user did also suggest testing to a different FTP server as well as making sure firewalls between the connection were not the issue.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...