我使用loopj / android-async-http lib在我的服务器上传文件。
我的Android代码:
File myFile = new File(getRealPathFromURI(SettintsActiviy.this, selectedImage));
RequestParams params = new RequestParams();
try {
params.put("uploaded_file", myFile);
params.put("name", "adfadfa");
params.put("tmp_name", "fadfadfa");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://localhost:8081/html/upload.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println("statusCode "+statusCode);//statusCode 200
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
我的upload.php
<?php
$file_path = "/images/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
}?>
记录Android结果:
06-09 22:19:53.192 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 205268 from 221141 (93%)
06-09 22:19:53.192 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 209364 from 221141 (95%)
06-09 22:19:53.192 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 213460 from 221141 (97%)
06-09 22:19:53.192 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 217556 from 221141 (98%)
06-09 22:19:53.192 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 221103 from 221141 (100%)
06-09 22:19:53.193 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 221105 from 221141 (100%)
06-09 22:19:53.193 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 221141 from 221141 (100%)
06-09 22:19:56.252 18958-18958/br.md.screamchat V/AsyncHttpRH: Progress 7 from 7 (100%)
06-09 22:19:56.252 18958-18958/br.md.screamchat I/System.out: statusCode 200
但是当我打开所选文件夹时没有文件。