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
 
     
    