我想使用 PHP post 将文件从 android 设备上传到服务器。现在我研究了多部分实体上传并修复了我的 android 设备上的所有内容,但它仍然无法正常工作,我只是不知道为什么,因为这是我第一次尝试为 android 执行此操作。现在我拥有的是这种方法:
public void postFile(String file_path, String url){
File file = new File(file_path);
try {
System.out.println(file_path);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bin); /*I believe that the "file" is the name in php part*/
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
我使用 onclick 侦听器以这种格式调用此方法
postFile(record_location, "http://10.146.179.86/File%20upload/test.php");
记录位置在哪里:/mnt/sdcard/Echo/Recordings/Awesome4.3gpp
和 PHP 上传代码:
<?php
$target_path = "Stored File/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
我已经尝试过上传并且它正在工作,这就是为什么我认为问题出在我的 android 方法中。顺便说一下,这是logcat:
06-20 06:38:39.662 37-88/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-20 06:38:39.912 278-294/system_process I/ActivityManager: Displayed com.obpg.echo/.PlaybackEditActivity: +704ms
06-20 06:38:41.933 3399-3399/com.obpg.echo I/System.out: /mnt/sdcard/Echo/Recordings/Awesome4.3gpp
06-20 06:38:42.132 3399-3399/com.obpg.echo W/System.err: android.os.NetworkOnMainThreadException
06-20 06:38:42.213 3399-3399/com.obpg.echo W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:112)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at java.net.Socket.connect(Socket.java:842)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-20 06:38:42.222 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-20 06:38:42.233 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-20 06:38:42.243 3399-3399/com.obpg.echo W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-20 06:38:42.243 3399-3403/com.obpg.echo D/dalvikvm: GC_CONCURRENT freed 347K, 12% free 3688K/4156K, paused 77ms+103ms, total 254ms
06-20 06:38:42.252 3399-3399/com.obpg.echo W/System.err: at com.obpg.echo.PlaybackEditActivity.postFile(PlaybackEditActivity.java:279)
06-20 06:38:42.252 3399-3399/com.obpg.echo W/System.err: at com.obpg.echo.PlaybackEditActivity$3.onClick(PlaybackEditActivity.java:126)
06-20 06:38:42.252 3399-3399/com.obpg.echo W/System.err: at android.view.View.performClick(View.java:4204)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.view.View$PerformClick.run(View.java:17355)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.os.Handler.handleCallback(Handler.java:725)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.os.Looper.loop(Looper.java:137)
06-20 06:38:42.263 3399-3399/com.obpg.echo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 06:38:42.273 3399-3399/com.obpg.echo W/System.err: at dalvik.system.NativeStart.main(Native Method)
我在这里错过了什么吗?另外,也许有人可以向我解释这个 entity.addPart() 是如何工作的,因为我不知道它谈论的内容主体是什么以及我应该指定什么字符串。