title: Uploading files to server in Android
body: <p>I did upload a text file from C drive to server using java code..
  I included the same code in android and executed. But i am unable to upload the file.</p>
  
  <p>I am getting these warnings in catlog:</p>
  
  <pre><code>&gt;08-07 17:39:12.360: WARN/System.err(32030): java.io.IOException: Unable to connect to server: Unable to retrieve file: 550
  &gt;08-07 17:39:12.391: WARN/System.err(32030):     at org.apache.harmony.luni.internal.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:203)
  &gt;08-07 17:39:12.401: WARN/System.err(32030):     at org.apache.harmony.luni.internal.net.www.protocol.ftp.FtpURLConnection.getOutputStream(FtpURLConnection.java:344)
  &gt;08-07 17:39:12.411: WARN/System.err(32030):     at f.t.p.Upload.upload_file(Upload.java:26)
  &gt;08-07 17:39:12.431: WARN/System.err(32030):     at f.t.p.FTP.onCreate(FTP.java:24)
  &gt;08-07 17:39:12.445: WARN/System.err(32030):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  &gt;08-07 17:39:12.450: WARN/System.err(32030):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
  &gt;08-07 17:39:12.460: WARN/System.err(32030):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
  &gt;08-07 17:39:12.460: WARN/System.err(32030):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
  &gt;08-07 17:39:12.510: WARN/System.err(32030):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
  &gt;08-07 17:39:12.520: WARN/System.err(32030):     at android.os.Handler.dispatchMessage(Handler.java:99)
  &gt;08-07 17:39:12.530: WARN/System.err(32030):     at android.os.Looper.loop(Looper.java:123)
  &gt;08-07 17:39:12.530: WARN/System.err(32030):     at android.app.ActivityThread.main(ActivityThread.java:4363)
  &gt;08-07 17:39:12.530: WARN/System.err(32030):     at java.lang.reflect.Method.invokeNative(Native Method)
  &gt;08-07 17:39:12.540: WARN/System.err(32030):     at java.lang.reflect.Method.invoke(Method.java:521)
  &gt;08-07 17:39:12.551: WARN/System.err(32030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  &gt;08-07 17:39:12.571: WARN/System.err(32030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  &gt;08-07 17:39:12.580: WARN/System.err(32030):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I changed the FileInputStream to /sdcard/test.txt and given .INTERNET, .WRITE_EXTERNAL_STORAGE permissions in AndroidManifest.xml</p>
  
  <blockquote>
    <p>used: <a href="ftp://user:pass@www.site.com/test.txt;type=i" rel="nofollow noreferrer">ftp://user:pass@www.site.com/test.txt;type=i</a>" as a url to connect.</p>
  </blockquote>
  
  <p>I do not understand why the code does not work in android while it worked in Java. Do I need to add any extra code for android?</p>
  
  <p>Please help me... Thank you... :)</p>
  
  <p>Here is the code..</p>
  
  <pre><code>public class Upload {
  
      public static void upload_file() throws MalformedURLException, IOException{
  
  
           BufferedInputStream bis = null;
           BufferedOutputStream  bos = null;
           try
           {
  
              URL url = new URL("ftp://user:pass@www.site.com/test.txt;type=i");
  
              URLConnection urlc = url.openConnection();
  
              System.out.println("COnnection established:");
  
                  bos = (BufferedOutputStream) urlc.getOutputStream(); 
  
                  bis = new BufferedInputStream(new FileInputStream("/sdcard/test.txt"));
                    byte[] readData = new byte[1024];
                    int length;
                    while((length=bis.read(readData))&gt;0) {
                      bos.write(readData);
                    }
                    bis.close();
                    bos.flush();
                    bos.close();
           }catch(IllegalStateException ise){  System.out.println("Illegal state exception for setUseCaches.."); }
           finally
           {
              if (bis != null)
                 try{
                    bis.close();
                    System.out.println("closed bis");
                 }
                 catch (IOException ioe){}
              if (bos != null)
                 try{
                    bos.close();
                    System.out.println("closed bos");
                 }catch (IOException ioe){                   
                      System.out.println("Exception, not finally..");
                 }
           }
        }
  }
  </code></pre>