manidhar mulaparthi
manidhar mulaparthi

Reputation: 1082

Uploading files to server in Android

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.

I am getting these warnings in catlog:

>08-07 17:39:12.360: WARN/System.err(32030): java.io.IOException: Unable to connect to server: Unable to retrieve file: 550
>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)
>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)
>08-07 17:39:12.411: WARN/System.err(32030):     at f.t.p.Upload.upload_file(Upload.java:26)
>08-07 17:39:12.431: WARN/System.err(32030):     at f.t.p.FTP.onCreate(FTP.java:24)
>08-07 17:39:12.445: WARN/System.err(32030):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
>08-07 17:39:12.450: WARN/System.err(32030):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
>08-07 17:39:12.460: WARN/System.err(32030):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
>08-07 17:39:12.460: WARN/System.err(32030):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
>08-07 17:39:12.510: WARN/System.err(32030):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
>08-07 17:39:12.520: WARN/System.err(32030):     at android.os.Handler.dispatchMessage(Handler.java:99)
>08-07 17:39:12.530: WARN/System.err(32030):     at android.os.Looper.loop(Looper.java:123)
>08-07 17:39:12.530: WARN/System.err(32030):     at android.app.ActivityThread.main(ActivityThread.java:4363)
>08-07 17:39:12.530: WARN/System.err(32030):     at java.lang.reflect.Method.invokeNative(Native Method)
>08-07 17:39:12.540: WARN/System.err(32030):     at java.lang.reflect.Method.invoke(Method.java:521)
>08-07 17:39:12.551: WARN/System.err(32030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
>08-07 17:39:12.571: WARN/System.err(32030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
>08-07 17:39:12.580: WARN/System.err(32030):     at dalvik.system.NativeStart.main(Native Method)

I changed the FileInputStream to /sdcard/test.txt and given .INTERNET, .WRITE_EXTERNAL_STORAGE permissions in AndroidManifest.xml

used: ftp://user:[email protected]/test.txt;type=i" as a url to connect.

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?

Please help me... Thank you... :)

Here is the 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:[email protected]/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))>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..");
               }
         }
      }
}

Upvotes: 1

Views: 7047

Answers (3)

Vasily Avilov
Vasily Avilov

Reputation: 163

If you like to use URLConnection it is necessary to disable input stream. For example:

    URLConnection urlc = url.openConnection();
    urlc.setDoInput(false);
    urlc.setDoOutput(true);

Upvotes: 0

Tran Huu Phuoc
Tran Huu Phuoc

Reputation: 117

FPT protocol is not support in some handset devices. Should be use http to upload file instead of fpt.

If you want to upload a file to your server. You should use WCF REST Service. Currently, I still don't know how to do with WCF REST Service but I think it is possible.

Upvotes: 0

reynev
reynev

Reputation: 336

Remove:

;type=i

from:

 URL url = new URL("ftp://user:[email protected]/test.txt;type=i");

It works in my application, I've just solved it.

Upvotes: 1

Related Questions