本地拍照,发送到服务器 "POST"
以二进制byte[]方式发送
一张图片大概为300K左右,一次发送完毕
//post请求 @SuppressLint("HandlerLeak") public static boolean postRequest(String urlPath, byte[] b){ try{ URL url=new URL(urlPath); HttpURLConnection con=(HttpURLConnection)url.openConnection(); con.setRequestMethod("POST"); con.setReadTimeout(5*1000); con.setDoOutput(true);//打开向外输出 con.setRequestProperty("Content-Type", HTTP.UTF_8);//内容类型 con.setRequestProperty("Content-Length",String.valueOf(b.length));//长度 OutputStream outStream=con.getOutputStream(); outStream.write(b);//写入数据 outStream.flush();//刷新内存 outStream.close(); //状态码是不成功 if(con.getResponseCode()==200){ Log.e("result","传输成功"); return true; } else{ Log.v("result", "code = " + con.getResponseCode()); } }catch(Exception e){ e.printStackTrace(); } return false; }
//耗时操作,开启一个线程 private Runnable mRunnable = new Runnable() { public void run() { String ip = "http://192.168.1.100/ZD_BaseDataTrans_SQL/UploadPicture.aspx"; //httpClientpost(imgToBase64(cameraPath, null, null), ip); postRequest(ip, getImgString(cameraPath, null, null)); mHandler2.sendMessage(mHandler.obtainMessage()); } };
cameraPath为相片保存的本地路径
时间: 2024-11-05 18:34:25