0

I am trying to upload a file to an FTP-server using a URLConnection. There are no exceptions thrown. I seem to be able to connect to the server. I can download the file (with another peace of the program) without problems if I upload it "manually" (with an FTP client).

I use BufferedOutputStream.write(int). The log shows that I can read the local file (which contains "Hej hej") and access the bytes in it. Problem is: nothing is written to the server file. When I read the file on the server, it still contains the pre-upload content. The file isn't updated. I tried BufferedOutputStream.write(String.getBytes()) but that didn't help.

static public void upload(String string, String file) {
    File file_ = new File(Environment.getExternalStorageDirectory() + "/Android/data/org.sionlinkoping.pingstprayer/files/thanks.txt");
try {
    upload("ftphome.mah.se", user, passwd, "thanks.txt", file_);
}
catch(Exception e) { Log.d("",e.toString()); }
}


static 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 );

        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)
        {
            Log.d("","i:"+i);
            bos.write( i );
        }
        bos.flush();
    }
    finally
    {
        if (bis != null)
            try
            {
                bis.close();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
        if (bos != null)
            try
            {
               bos.close();
            }
            catch (IOException ioe)
            {
                ioe.printStackTrace();
            }
    }
    }
else
{
    System.out.println( "Input not available." );
}
}

The Log shows:

01-07 14:00:50.662: DEBUG/(6925): i:239
01-07 14:00:50.662: DEBUG/(6925): i:187
01-07 14:00:50.662: DEBUG/(6925): i:191
01-07 14:00:50.662: DEBUG/(6925): i:72
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.662: DEBUG/(6925): i:32
01-07 14:00:50.662: DEBUG/(6925): i:104
01-07 14:00:50.662: DEBUG/(6925): i:101
01-07 14:00:50.662: DEBUG/(6925): i:106
01-07 14:00:50.672: DEBUG/(6925): i:10
JonatanEkstedt
  • 905
  • 1
  • 8
  • 27

1 Answers1

2

The problem lies with your assumption that writing to an FTP URL will result in a file being saved to the server. It won't. The results are just undefined -- it doesn't work that way.

There are FTP libraries available; you'll need to use one. For example, see here.

Community
  • 1
  • 1
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    Ok, but the file is already there. I just want to add to it. Still need a library? – JonatanEkstedt Jan 07 '12 at 14:07
  • You need a library (or write your own that utilizes the FTP protocol specs, if you are a masochist) to do anything with FTP. – Scott Faria Jan 07 '12 at 14:33
  • 1
    Aha! Is that a Java specific fact? This program I'm developing is supposed to be an Android app. I wrote the same app for iPhone earlier, in Objective-C, and I didn't seem to need any extra library, then? Maybe Objective-C has that standard funcionality or something? – JonatanEkstedt Jan 07 '12 at 14:41
  • 1
    Btw, I can still read a file on the FTP server without the library. – JonatanEkstedt Jan 07 '12 at 14:55
  • 1
    "Is that a Java-specific fact?" I guess so. There's no law that says other platforms couldn't have been implemented such that their `URLConnection` equivalent could handle ftp uploads. But Java's isn't designed that way, in any event -- it's read-only. – Ernest Friedman-Hill Jan 07 '12 at 18:28