android上傳圖片到PHP後臺全過程

PS:便宜的服務器可是會不定時的坑你一把。     

今天在修改app的一些交互以及重構代碼。一切都是那麼順利,啪啪啪,runing,測試沒問題,再啪啪啪。。。

突然,測試上傳頭像的時候,老是連接超時。。。。報如下錯誤:

XXXXXXSokcetTimeOutXXXXXXXX

然後自己設置HTTP的超時時間:

  //設置超時時間
  httpclient.setTimeout(20000);

再building,runing,還是不行。。。。這就怪了,明明好好的,怎麼會突然就變成連接超時了呢!又折騰了一陣子後,也跟後臺那邊的朋友溝通過,他也測試了上傳接口,發現沒什麼問題,就讓我自己去折騰去了。。。。

我就鬱悶了,看不出原先的代碼有什麼錯誤,也沒什麼法子了,就出最下下策吧,自己搭一個PHP上傳圖片接口,親自測試下到底是怎麼回事。。。。


1.首先,你得下一個方便快捷的PHP服務器,我這裏用了WampServer,百度----下載-----安裝-----啓動,瀏覽器輸入:http://127.0.0.1 有頁面顯示,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


PS:2016-07-12  發現新版本的WarmServer按照上面的配置不行了,解決方法如下圖:(注意文件路徑)



添加紅色框框部分


3. 再此輸入 : http://本機IP地址    回車    顯示頁面    OK!   至於爲什麼要第二步、第三步呢,我就不說了。。。留給新人去想想吧! 大神直接無視。。。。。

4.寫一個上傳圖片的PHP文件,當然我一個敲java的孩子一下子怎麼可能憋的出來,那怎麼辦,當然是百度參考別人的了,下面的PHP代碼源自網絡,親測沒有錯誤:

<?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了,代碼段如下:

	       //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="http://192.168.0.8/upload.php";   這個192.168.0.8是我的PHP部署的地址,改成你自己的就行了。


PS:別犯2,用了127.0.0.1    想想爲啥不能用127.0.0.1

到此就是building,runing了。  發現OK。。。。   可以上傳,並在www目錄下找到upload目錄,upload目錄下有上傳的圖片。。。。




7.這就納悶了。。。。 我又鼓起勇氣找了PHP後端,跟他激烈的討論一番後,發現是服務器坑了爹啊!  800塊一年的服務器。。。。。唉。。。不說了。。。。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章