[php] view plaincopyprint?<?php $base_path = "./upload/"; //存放目录 if(!is_dir($base_path)){ mkdir($base_path,0777,true); } $target_path = $base_path . basename ( $_FILES ["attach"] ["name"] ); if (move_uploaded_file ( $_FILES ["attach"] ["tmp_name"], $target_path )) { $array = array ( "status" => true, "msg" => $_FILES ["attach"] ["name"]); echo json_encode ( $array ); } else { $array = array ( "status" => false, "msg" => "There was an error uploading the file, please try again!" . $_FILES ["attach"] ["error"]); echo json_encode ( $array ); } ?> 5.将上面的php文件放在WM安装目录下的www目录下,我的如下图所示,仅供参考:
6.经过上面几个步骤,PHP端已经搭建好了,现在就是回到android端改改IP地址测试下就oK了,代码段如下:
[java] view plaincopyprint? //HTTP上传图片 RequestParams params = new RequestParams(); try { //将压缩后的bitmap保存为图片文件 String saveImgPath=getSD_Path()+"/saveimg.png"; File saveimg=new File(saveImgPath); FileOutputStream fos = new FileOutputStream(saveimg); bmp.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); //上传压缩后的文件,大约100k左右 File uploadImg=new File(saveImgPath); <span style="color:#ff0000;">params.put("attach", uploadImg);</span> } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //上传地址 String url=URLConfigs.UploadHeadImage_ukey+myprefs.Ukey().get(); <span style="color:#ff0000;">String url="http://192.168.0.8/upload.php";</span> LogUtil.e(TAG, "upload img url :"+url); AsyncHttpUtil.post_loading(context,url, params, new MyTextHttpResponseHandler() { @Override public void onSuccess(int status, Header[] arg1, String json) { super.onSuccess(status, arg1, json); LogUtil.e(TAG, "上传图片 json :"+json); RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class); if(entity.isStatus()){ //上传成功,设置图片 face.setImageBitmap(bmp); ToastUtils.show(context, "上传成功"); }else{ ToastUtils.show(context, json); }myprefs.position().put(0); }@Override public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) { super.onFailure(arg0, arg1, arg2, arg3); myprefs.position().put(0); arg3.printStackTrace(); ToastUtils.show(context, R.string.network_unavailable); } params.put("attach", uploadImg); 这里的attach参数是和服务端一一对应的,别乱改。。。。
7.这就纳闷了。。。。 我又鼓起勇气找了PHP后端,跟他激烈的讨论一番后,发现是服务器坑了爹啊! 800块一年的服务器。。。。。唉。。。不说了。。。。