当前位置: 移动技术网 > IT编程>开发语言>PHP > android上传图片到PHP的过程详解

android上传图片到PHP的过程详解

2018年05月07日  | 移动技术网IT编程  | 我要评论

今天在做上传头像的时候,总是提交连接超时错误,报错信息如下:xxxxxxsokcettimeoutxxxxxxxx

然后自己设置http的超时时间:

复制代码 代码如下:

[java] view plaincopyprint?
//设置超时时间 
httpclient.settimeout(20000); 

再building,runing,还是不行。。。。这就怪了,明明好好的,怎么会突然就变成连接超时了呢!又折腾了一阵子后,也跟后台那边的朋友沟通过,他也测试了上传接口,发现没什么问题,就让我自己去折腾去了。。。。
我就郁闷了,看不出原先的代码有什么错误,也没什么法子了,就出最下下策吧,自己搭一个php上传图片接口,亲自测试下到底是怎么回事。。。。

1.首先,你得下一个方便快捷的php服务器,我这里用了wampserver,百度----下载-----安装-----启动,浏览器输入: 有页面显示,ok了。就这么简单!

2.浏览器输入 : http://本机ip地址    回车,   发现报错,类似“you don't have permission to access / on this server”  说明你的wm还没设置,需要进行如下设置:
造成这个问题的原因是apache 的http.conf内的默认配置是

复制代码 代码如下:

# onlineoffline tag - don't remove
order deny,allow
deny from all
allow from 127.0.0.1

只允许127.0.0.1访问,点击wampserver图标让后点击putonline,http.conf内的以上默认配置自动修改为
复制代码 代码如下:

# onlineoffline tag - don't remove
order allow,deny
allow from all

现在localhost可以访问了。

同样phpmyadmin在localhost下不能正常访问在127.0.0.1能正常访问,解决方法:
点击根目录下的alias目录,打开phpmyadmin.conf配置文件,和上面修改http.conf一样把

复制代码 代码如下:

deny from all
allow from 127.0.0.1
修改为
allow from all

3. 再此输入 : http://本机ip地址    回车    显示页面    ok!   至于为什么要第二步、第三步呢,我就不说了。。。留给新人去想想吧! 大神直接无视。。。。。
4.写一个上传图片的php文件,当然我一个敲java的孩子一下子怎么可能憋的出来,那怎么办,当然是百度参考别人的了,下面的php代码源自网络,亲测没有错误:

[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参数是和服务端一一对应的,别乱改。。。。
string url="";   这个192.168.0.8是我的php部署的地址,改成你自己的就行了。


ps:别犯2,用了127.0.0.1    想想为啥不能用127.0.0.1

到此就是building,runing了。  发现ok。。。。   可以上传,并在www目录下找到upload目录,upload目录下有上传的图片。。。。


7.这就纳闷了。。。。 我又鼓起勇气找了php后端,跟他激烈的讨论一番后,发现是服务器坑了爹啊!  800块一年的服务器。。。。。唉。。。不说了。。。。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网