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..");
}
}
}
}