java post 流_java-HttpGetPost-图片字节流上传

/*** 不显示的使用HttpPost就会默认提交请求的方式为post,

* 传递的参数为以key:value的形式来传递参数

*@paramhttpUrl

*@paramimagebyte 可以传递图片等文件,以字节数组的形式传递*/@SuppressWarnings({"deprecation"})public static String httpPost(String httpUrl,byte[] imagebyte){

httpUrl="http://192.168.199.138/weixin/test.php";

HttpPost httpPost= newHttpPost(httpUrl);ByteArrayBody image = new ByteArrayBody(imagebyte,ContentType.APPLICATION_JSON,"image.jpg");//传递图片的时候可以通过此处上传image.jpg随便给出即可

String appid= "appid";

String secret= "secret";

StringBody appidbody= newStringBody(appid,ContentType.APPLICATION_JSON);

StringBody secretbody= newStringBody(secret,ContentType.APPLICATION_JSON);

MultipartEntityBuilder me=MultipartEntityBuilder.create();me.addPart("image", image)//image参数为在服务端获取的key通过image这个参数可以获取到传递的字节流,这里不一定就是image,你的服务端使用什么这里就对应给出什么参数即可

.addPart("appid",appidbody )

.addPart("secret", secretbody);

DefaultHttpClient client= newDefaultHttpClient();

HttpEntity reqEntity=me.build();

httpPost.setEntity(reqEntity);

HttpResponse responseRes= null;try{

responseRes=client.execute(httpPost);

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

client.close();

}int status =responseRes.getStatusLine().getStatusCode();

String resultStr=null;if (status == 200) {byte[] content;try{

content=getContent(responseRes);

resultStr= new String(content,"utf-8");

System.out.println("httpPost返回的结果==:"+resultStr);

}catch(IOException e) {

e.printStackTrace();

}

}if(resultStr !=null){returnresultStr;

}else{return "";

}

}/*** 显示的调用HttpPost来使用post方式提交请求,并且将请求的参数提前拼装成json字符串,

* 直接使用StringEntity来发送参数不必要再使用key:value的形式来设置请求参数,

* 在服务端使用request.getInputStream()来把request中的发送过来的json字符串解析出来,

* 就是因为使用StringEntity来包装了传递的参数

*@paramhttpUrl

*@paramjsonParam*/@SuppressWarnings({"resource", "deprecation"})public staticString httpPostShow(String httpUrl,String jsonParam){

httpUrl="http://192.168.199.138/weixin/test.php";

jsonParam="{\"appid\":\"appidbbbb33333\",\"secret\":\"secretaaaaa3333\"}";//拼装一个json串

DefaultHttpClient httpClient= newDefaultHttpClient();//这里就是显示的调用post来设置使用post提交请求

HttpPost method = newHttpPost(httpUrl);

String result= null;try{if (null !=jsonParam) {//解决中文乱码问题

StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");//这个StringEntity可以在服务端不用key:value形式来接收,可以request.getInputStream()来获取处理整个请求然后解析即可

entity.setContentEncoding("UTF-8");

entity.setContentType("application/json");

method.setEntity(entity);

}

HttpResponse response=httpClient.execute(method);

httpUrl= URLDecoder.decode(httpUrl, "UTF-8");if (response.getStatusLine().getStatusCode() == 200) {try{

result=EntityUtils.toString(response.getEntity());

}catch(Exception e) {

e.printStackTrace();

}

}

}catch(IOException e) {

e.printStackTrace();

}if(result !=null){returnresult;

}else{return "";

}

}private static byte[] getContent(HttpResponse response)throwsIOException {

InputStream result= null;

ByteArrayOutputStream out= newByteArrayOutputStream();try{

HttpEntity resEntity=response.getEntity();if (resEntity != null) {

result=resEntity.getContent();int len = 0;while ((len = result.read()) != -1) {

out.write(len);

}returnout.toByteArray();

}

}catch(IOException e) {

e.printStackTrace();throw new IOException("getContent异常", e);

}finally{

out.close();if (result != null) {

result.close();

}

}return null;

}


版权声明:本文为weixin_26732855原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。