android上传图片到PHP的过程详解 |
今天在做上传头像的时候,总是提交连接超时错误,报错信息如下:XXXXXXSokcetTimeOutXXXXXXXX 然后自己设置HTTP的超时时间: 复制代码 代码如下: [java] view plaincopyprint? //设置超时时间 httpclient.setTimeout(20000); 再building,runing,还是不行 。 。 。 。这就怪了,明明好好的,怎么会突然就变成连接超时了呢!又折腾了一阵子后,也跟后台那边的朋友沟通过,他也测试了上传接口,发现没什么问题,就让我自己去折腾去了 。 。 。 。 1.首先,你得下一个方便快捷的PHP服务器,我这里用了WampServer,百度----下载-----安装-----启动,浏览器输入:http://127.0.0.1 有页面显示,OK了 。就这么简单! 2.浏览器输入 : http://本机IP地址 回车, 发现报错,类似“You dont have permission to access / on this server” 说明你的WM还没设置,需要进行如下设置: 复制代码 代码如下: # onlineoffline tag - dont remove Order Deny,Allow Deny from all Allow from 127.0.0.1 只允许127.0.0.1访问,点击wampserver图标让后点击Putonline,http.conf内的以上默认配置自动修改为 复制代码 代码如下: # onlineoffline tag - dont remove Order Allow,Deny Allow from all 现在localhost可以访问了 。 同样phpMyadmin在localhost下不能正常访问在127.0.0.1能正常访问,解决方法: 复制代码 代码如下: Deny from all Allow from 127.0.0.1 修改为 Allow from all 3. 再此输入 : http://本机IP地址 回车 显示页面 OK! 至于为什么要第二步、第三步呢,我就不说了 。 。 。留给新人去想想吧! 大神直接无视 。 。 。 。 。 [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参数是和服务端一一对应的,别乱改 。 。 。 。
到此就是building,runing了 。 发现OK 。 。 。 。 可以上传,并在www目录下找到upload目录,upload目录下有上传的图片 。 。 。 。
|