The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Java


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 11-14-2007, 03:01 AM
zaher1988 zaher1988 is offline
Novice
Join Date: Oct 2007
Posts: 11
iTrader: (0)
zaher1988 is on a distinguished road
FtpProtocolException: PORT :500

While my FTP upload class is attempting to upload a file, it breaks and get this error

Quote:
Exception in thread "main" sun.net.ftp.FtpProtocolException: PORT :500 Illegal PORT command, EPSV ALL in effect

at sun.net.ftp.FtpClient.openDataConnection(FtpClient .java:435)
at sun.net.ftp.FtpClient.put(FtpClient.java:594)
at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream( FtpURLConnection.java:482)
at FileUpload.upload(Play1.java:114)
at Play1.main(Play1.java:56)
Java Result: 1
To be more specific, after several attempts of running the java program or applet again, it works. So it is like random, sometimes it works sometimes i get this error.

My class is the following

Code:
class FileUpload
{
   /**
    * Upload a file to a FTP server. A FTP URL is generated with the
    * following syntax:
    * ftp://user:password@host:port/filePath;type=i.
    * 
    * @param ftpServer , FTP server address (optional port ':portNumber').
    * @param user , Optional user name to login.
    * @param password , Optional password for user.
    * @param fileName , Destination file name on FTP server (with optional
    *            preceding relative path, e.g. "myDir/myFile.txt").
    * @param source , Source file to upload.
    * @throws MalformedURLException, IOException on error.
    */
   public void upload( String ftpServer, String user, String password, String fileName, File source ) 
         throws MalformedURLException,
         IOException
   {
      if (ftpServer != null && fileName != null && source != null)
      {
         StringBuffer sb = new StringBuffer( "ftp://" );
         // check for authentication else assume its anonymous access.
         if (user != null && password != null)
         {
            sb.append( user );
            sb.append( ':' );
            sb.append( password );
            sb.append( '@' );
         }
         sb.append( ftpServer );
         sb.append( '/' );
         sb.append( fileName );
         /*
          * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
          * listing
          */
         sb.append( ";type=a" );
          System.out.println(sb);
         BufferedInputStream bis = null;
         BufferedOutputStream bos = null;
         try
         {
            URL url = new URL( sb.toString() );
            URLConnection urlc = url.openConnection();
            bos = new BufferedOutputStream( urlc.getOutputStream() );
            bis = new BufferedInputStream( new FileInputStream( source ) );
            int i;
            // read byte by byte until end of stream
            while ((i = bis.read()) != -1)
            {
               bos.write( i );
            }
         }
         finally
         {
            if (bis != null)
            {
                 try
                 {
                     bis.close();
                 }
                 catch (IOException e)
                 {
                    bis = null;
                 }
            }        
            if (bos != null)
            {
                 try
                 {
                     bos.close();
                 }
                 catch (IOException e)
                 {
                    bos = null;
                 }    
            }
         }
      }
      else
      {
         System.out.println( "Input not available." );
      }
   }
}
I read somewhere that this might be cause by the fact that the class uses active ftp mode when it has to use the passive one, i'm not sure about this though.

So does any body got an idea why i get this error from time to time?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by zaher1988 : 11-14-2007 at 03:11 AM.
Reply With Quote
  #2 (permalink)  
Old 11-14-2007, 06:42 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
There is a specific protocol associated with FTP. There are variations possible with that protocol. Quite a few actually.

Even within that protocol there can be variations based on exactly which commands a specific ftp server expects. In general purpose ftp clients this is handle by providing varying options.

sun.net.www.protocol.ftp doesn't implement all of the possibilities. It is possible that it only implements one. So it either works or it doesn't.

If it doesn't then you need to try another solution. Such as the jakarta commons net solution. Even with that you still might need to experiment with a particular server.

Edit:
Java Programming - FtpProtocolException: PORT :500
JavaWorld Forums: error in ftp code
Socket Programming - problem with sun.net.ftp

Last edited by Lee : 11-14-2007 at 01:37 PM. Reason: Edit your last post!
Reply With Quote
  #3 (permalink)  
Old 11-14-2007, 08:34 AM
zaher1988 zaher1988 is offline
Novice
Join Date: Oct 2007
Posts: 11
iTrader: (0)
zaher1988 is on a distinguished road
Thank you for your replies.

Actually i passed over the suggested links before and i got this same reply on a thread i posted on Java Programming forums which you have also included in your links post.

Anyway let's focus on switching to passive mode rather than active. How can my class use passive mode? what should we change in it?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 11-14-2007, 09:19 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
It supposedly tries PASV mode first... and hits PORT mode if it has to.

I would try out: FtpBean
Reply With Quote
  #5 (permalink)  
Old 11-16-2007, 12:29 AM
zaher1988 zaher1988 is offline
Novice
Join Date: Oct 2007
Posts: 11
iTrader: (0)
zaher1988 is on a distinguished road
Quote:
Originally Posted by ccoonen View Post
It supposedly tries PASV mode first... and hits PORT mode if it has to.

I would try out: FtpBean

I have read about FtpBean before, never thought of trying it, anyway what i did now is a mix between my class and FtpBean, so that when any fails the program will try the other method, and it works!.

Thank you for your cooperation.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 11-16-2007, 05:57 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Good work buddy
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:01 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


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