我確實上傳了一個文本文件從C盤到使用java代碼的服務器.. 我在android中包含相同的代碼並執行。但我無法上傳文件。在Android上傳文件到服務器
我得到這些警告在目錄下載:
>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)
我改變了對的FileInputStream和/sdcard/test.txt給出。互聯網,.WRITE_EXTERNAL_STORAGE權限在AndroidManifest.xml
使用: ftp://user:[email protected]/test.txt;type=i「作爲url連接。
我不明白爲什麼代碼在android中不起作用Java的。我是否需要爲android添加任何額外的代碼?
請幫我...謝謝... :)
這裏是代碼..
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..");
}
}
}
}
如果您要我添加代碼,請告訴我,我也會添加該代碼。爲什麼沒有人回答這個問題。 :) – 2010-08-09 06:20:40