将文件上传到Android中的服务器
我确实使用java代码从C驱动器上传了一个文本文件到服务器。。 我在android中包含了相同的代码并执行了。但是我无法上传文件 我在catlog中收到以下警告:将文件上传到Android中的服务器,android,file-upload,Android,File Upload,我确实使用java代码从C驱动器上传了一个文本文件到服务器。。 我在android中包含了相同的代码并执行了。但是我无法上传文件 我在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(3
>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)
我将文件inputstream更改为/sdcard/test.txt,并在AndroidManifest.xml中授予.INTERNET、.WRITE\u EXTERNAL\u存储权限
使用:“作为连接的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: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))>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..");
}
}
}
}
删除:
;type=i
发件人:
它在我的应用程序中起作用,我刚刚解决了它。删除:
;type=i
发件人:
它在我的应用程序中工作,我刚刚解决了它。某些手持设备不支持FPT协议。 应该使用http来上传文件,而不是fpt 如果要将文件上载到服务器,应使用WCF REST服务。
目前,我仍然不知道如何使用WCF REST服务,但我认为这是可能的。某些手机设备不支持FPT协议。 应该使用http来上传文件,而不是fpt 如果要将文件上载到服务器,应使用WCF REST服务。
目前,我仍然不知道如何使用WCF REST服务,但我认为这是可能的。如果您想使用URLConnection,则有必要禁用输入流。例如:
URLConnection urlc = url.openConnection();
urlc.setDoInput(false);
urlc.setDoOutput(true);
如果要使用URLConnection,则必须禁用输入流。例如:
URLConnection urlc = url.openConnection();
urlc.setDoInput(false);
urlc.setDoOutput(true);
如果你想让我添加代码,请告诉我,我也会添加。为什么没有人回答这个问题。:)如果你想让我添加代码,请告诉我,我也会添加。。为什么没有人回答这个问题。:)