commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Elcock <a...@elcock.org>
Subject Re: Commons NET
Date Tue, 09 Aug 2005 15:34:18 GMT
OK, so when I write to the FTP server with this code nothing appears at 
the other end.

If I use the "completePendingCommand" that is commented out things seem 
to hang.

Please excuse the code.

The upload is invoked when someone closes the output stream on the file 
that we are uploading (by means of OutputStreamWrapper).

Sure I am missing something, thanks -

Andrew
-------------------

   public void uploadFile(File f, boolean delete) {
     try {
//      mkparentdirs();
       if (!f.exists())
         return;

       FTPClient ftpChannel = getFTPChannel(getSite());
       try {
//        try {
//          ftpChannel.makeDirectory((new 
File(file)).getParentFile().getPath());
//        } catch (IOException e) {/* already existed */}
         FileInputStream fis = new FileInputStream(f);
         ftpChannel.storeFile(file, fis);
         fis.close();
//        ftpChannel.completePendingCommand();
System.out.print(ftpChannel.getReplyString());
         /* Don't think we want this in a finally block - may delete all 
copies! */
         if (delete)
           f.delete();
       } catch (IOException e) {
         e.printStackTrace();
       }
     } catch (Exception io) {
       io.printStackTrace();
     }
   }

   class OutputStreamWrapper extends FileOutputStream {
     private File f;
     public OutputStreamWrapper(File f) throws IOException {
       super(f);
       this.f = f;
     }

     public void close() throws IOException {
       super.close();
       uploadFile(f, true);
     }
   }

   private static Hashtable channelHash = new Hashtable();
   public static FTPClient getFTPChannel(Site site) {
     FTPClient chan = (FTPClient) channelHash.get(site);

     if (chan == null || !chan.isConnected()) {
       try {
         chan = new FTPClient();
         chan.connect(site.getRemoteHost());
         chan.login(site.getRemoteUser(), site.getRemotePassword());
         chan.enterRemotePassiveMode();

         channelHash.put(site, chan);
       } catch (Exception e) {
         e.printStackTrace();
         return null;
       }
     }
     return chan;
   }
----------------------

Knezevic, Mihael wrote:
> if u could describe your problem a bit more than perhaps someone on the list could help
u. just saying that it doesn't work is not very precise. perhaps a code snippet and a precise
description/error message will help us to get u on track.  
> 
> 
>>-----Original Message-----
>>From: Andrew Elcock [mailto:andy@elcock.org] 
>>Sent: Monday, August 08, 2005 11:07 AM
>>To: commons-user@jakarta.apache.org
>>Subject: Commons NET
>>
>>I am using much commons code and find it to be excellent and 
>>easy to use 
>>for the main, but have hit a barrier with the NET package.
>>
>>I am using the net code to upload files to an FTP server. Using the 
>>examples and documentation I have a system that can connect, create 
>>folders etc, but fails miserably to actually upload data. Is there a 
>>common error that I have fallen in to the trap of without luck from 
>>archives, or is it unique to me? I can send the code, but did 
>>not want 
>>to pollute the list straight off.
>>
>>Thanks,
>>Andrew
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message