7

I'm trying to upload images to a FTP server (on my local PC) from Android Phone (HTC Desire HD). Images are going to FTP server but they are corrupted.

And the method (ftpClient.storeFile()) throws IOException (Bad File Number)

Please help me.

This is the corrupted image link:

http://imageshack.us/photo/my-images/820/komikb.jpg/

And this is the code:

FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect("192.168.2.14");
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        ftpClient.setSoTimeout(10000);
        ftpClient.enterLocalPassiveMode();
        if(ftpClient.login("Administrator", "xxxx"))
        {
            File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
            FileInputStream fs= new FileInputStream(sFile);
            String fileName = sFile.getName();
            Boolean result = ftpClient.storeFile("/ftpfile.atspace.co.uk/" + fileName, fs);
            String has = "";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
skaffman
  • 398,947
  • 96
  • 818
  • 769
osman katib
  • 135
  • 1
  • 14
  • I'd say it got shortened by a number of bytes, not really corrupted. – fvu May 31 '11 at 21:03
  • +1 for the excellent circle on the (otherwise lovely) hydrangea. – sarnold May 31 '11 at 21:04
  • Is it a repeatable problem? Does that file always get corrupted identically? Do other files get similarly cropped / cut / mangled? – sarnold May 31 '11 at 21:05
  • 1
    there is no if the file is image. But if the file type is audio, audio file didn't play. – osman katib May 31 '11 at 21:07
  • yes this is a repeatable problem. I tried 2 type of file such as audio and image. Also I tried different images – osman katib May 31 '11 at 21:10
  • 1
    could it be that you got bitten by https://issues.apache.org/jira/browse/NET-409 ? – fvu May 31 '11 at 21:11
  • @osman: Does the link that fvu posted above apply to your situation? – Squonk May 31 '11 at 21:41
  • thank you so much.thank you so much.thank you so much.Problem is solved. everybody thank you:) – osman katib May 31 '11 at 21:58
  • 1
    @osman: Please explain how the problem was solved. It might help others with the same problem. You can do this by adding an answer yourself explaining what the issue was. – Squonk May 31 '11 at 22:22

2 Answers2

3

Apache FTP Client has several outstanding issues with this. Below are instructions on how to use Ftp4J to effectively handle ftp programatically though java.

Download Ftp4J: http://www.sauronsoftware.it/projects/ftp4j/download.php

Then in your IDE:

import java.io.File;
import java.io.IOException;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

public class FTP4J {

    /**
     * @param args
     * @throws FTPAbortedException 
     * @throws FTPDataTransferException 
     * @throws FTPException 
     * @throws FTPIllegalReplyException 
     * @throws IOException 
     * @throws IllegalStateException 
     */
    public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException {
        FTP4J ftp= new FTP4J();
        ftp.transfer();
    }

    private void transfer() throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException{
        FTPClient client = new FTPClient();
        client.connect("192.168.0.1"); //conect to FTP server (in my case a vsftp on centos 6.4)
        client.login("admn", "admn123");//login to FTP Server
        client.changeDirectory("/usr/share/tomcat/webapps/imgs/"); //tell FTP4J where on the Ftp Server to send your file that you want to upload.
        File fileUpload = new File ("C:\\Users\\ih8w8\\Pictures\\1.jpg"); //point FTP4J to the file you want to upload
        client.upload(fileUpload); //upload it
        client.disconnect(true); //close connection (note: you could also log out first, then disconn if youre not in a test env)
    }

}
Chris
  • 18,075
  • 15
  • 59
  • 77
2

Problem is solved. FTPClient class has "last packet data loss bug". But this was solved with 3.0.1 23.05.2011 release.

You can see from detailed explanation about bug: https://issues.apache.org/jira/browse/NET-409

You can download fixed relea https://repository.apache.org/content/repositories/snapshots/commons-net/commons-net/3.0.1-SNAPSHOT/

osman katib
  • 135
  • 1
  • 14
  • Hi osman i am using 3.0.1 with the code you have written above but it is not working for me. The image is getting uploaded but corrupted . png image not even displaying . Can you plz guide me??..Thanks – Sujit Jun 01 '11 at 13:43
  • 3
    Hi sujit, apache FTPClient has lots of bugs. This problem was solved. Another problems appeared later and I start using ftp4j. This is the link: http://www.sauronsoftware.it/projects/ftp4j/ I suggest you to use this component. – osman katib Jun 06 '11 at 20:07
  • are you sure that this ftp4j not raise problem? – Nikunj Patel Nov 15 '11 at 09:51