commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: [NET] Upload image problem
Date Mon, 23 May 2011 02:03:34 GMT
On 22 May 2011 15:19, Ehsun Behravesh <post@ehsunbehravesh.com> wrote:

In future, please start the subject with the Commons component, i.e.
[NET] as I have done in the reply.

> I use the following piece of code to upload a *photo* to a *ftp* host. But
> the photo seems *to be corrupted after being uploaded: There are narrow gray
> lines at the bottom of the photo.*
>
> The size of gray lines could be decreased by decreasing the Buffer Size of
> the FTPClient object.

Which version of Commons Net are you using?
What is the FTP server software?

Can you successfully upload the same file using a different FTP client?

> import java.io.File;
> import java.io.FileInputStream;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.apache.commons.net.ftp.FTPClient;
> import java.io.IOException;
> import java.io.InputStream;
> import org.apache.commons.net.ftp.FTP;
> import org.apache.commons.net.ftp.FTPReply;
> import sun.misc.Cleaner;
>
> public class FtpConnectDemo1 {
>  public static void main(String[] args) {
>    FTPClient client = new FTPClient();
>    try {
>      client.connect("ftp.ftpsite.com");
>      //
>      // When login success the login method returns true.
>      //
>      boolean login = client.login("user@ftpsite.com", "pass");
>      if (login) {
>        System.out.println("Login success...");
>        int replay = client.getReplyCode();
>        if (FTPReply.isPositiveCompletion(replay)) {
>          File file = new
> File("C:\\Users\\e.behravesh\\Pictures\\me2_rect.jpg");
>          FileInputStream input = new FileInputStream(file);
>          client.setFileType(FTP.BINARY_FILE_TYPE);

The setFileType method returns a boolean - you should check that it is true.

>          if (!client.storeFile(file.getName(), input)) {
>            System.out.println("upload failed!");
>          }
>          input.close();
>        }
>        //
>        // When logout success the logout method returns true.
>        //
>        boolean logout = client.logout();
>        if (logout) {
>          System.out.println("Logout from FTP server...");
>        }
>      } else {
>        System.out.println("Login fail...");
>      }
>    } catch (Exception e) {
>      e.printStackTrace();
>    } finally {
>      try {
>        //
>        // Closes the connection to the FTP server
>        //
>        client.disconnect();
>      } catch (IOException e) {
>        e.printStackTrace();
>      }
>    }
>  }
> }

The code looks OK - apart from failing to check if the setFileType
method has succeeded.

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


Mime
View raw message