java-HttpGetPost-图片字节流上传
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-HttpGetPost-图片字节流上传,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6062字,纯文字阅读大概需要9分钟。
内容图文
在java程序开发中经常用到与服务端的交互工作,主要的就是传递相应的参数请求从而获取到对应的结果加以处理
可以使用Get请求与Post请求,注意!这里的Get请求不是通过浏览器界面而是在程序代码中设置的,达到Get请求的目的,具体请详见下列描述
以下get与post请求需要引入的包:
import java.io.IOException; import java.io.InputStream; import java.net.URLDecoder; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;
Get请求:
/** * 正常简易的使用HttpGet来向服务端发送Get请求传递的参数直接在url后面拼装上就可以了 * @param httpUrl */ public static String httpGet(String httpUrl){ httpUrl ="http://192.168.199.138/weixin/test.php?appid=appidaaaa&secret=secretbbbb"; HttpGet httpGet = new HttpGet(httpUrl); CloseableHttpResponse chResponse = null; String result = null; try { chResponse = HttpClients.createDefault().execute(httpGet); if(chResponse.getStatusLine().getStatusCode()==200){ result = EntityUtils.toString(chResponse.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(chResponse !=null){ try { chResponse.close(); } catch (IOException e) { e.printStackTrace(); } } } if(result !=null){ System.out.println("有结果返回result=="+result); return result; }else{ System.out.println("请求没有结果返回"); return ""; } } /** * 显示的来调用HttpGet这里的发送执行execut与上面简易方法有不同, * 传递的参数直接拼装在url上传递即可 * @param httpUrl * @return*/ @SuppressWarnings("deprecation") publicstatic String httpGetShow(String httpUrl){ //httpUrl = ""; DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response =null; String result = null; try { //发送get请求 HttpGet httpGet = new HttpGet(httpUrl); //不同之处 response= client.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result = EntityUtils.toString(response.getEntity()); httpUrl = URLDecoder.decode(httpUrl, "UTF-8"); } } catch (IOException e) { e.printStackTrace(); }finally { client.close(); } if(result !=null){ return result; }else{ return ""; } }
Post请求:
/** * 不显示的使用HttpPost就会默认提交请求的方式为post, * 传递的参数为以key:value的形式来传递参数 * @param httpUrl * @param imagebyte 可以传递图片等文件,以字节数组的形式传递 */ @SuppressWarnings({ "deprecation" }) publicstatic String httpPost(String httpUrl,byte[] imagebyte){ httpUrl="http://192.168.199.138/weixin/test.php"; HttpPost httpPost = new HttpPost(httpUrl); ByteArrayBody image = new ByteArrayBody(imagebyte,ContentType.APPLICATION_JSON,"image.jpg");//传递图片的时候可以通过此处上传image.jpg随便给出即可 String appid = "appid"; String secret = "secret"; StringBody appidbody = new StringBody(appid,ContentType.APPLICATION_JSON); StringBody secretbody = new StringBody(secret,ContentType.APPLICATION_JSON); MultipartEntityBuilder me = MultipartEntityBuilder.create(); me.addPart("image", image)//image参数为在服务端获取的key通过image这个参数可以获取到传递的字节流,这里不一定就是image,你的服务端使用什么这里就对应给出什么参数即可 .addPart("appid",appidbody ) .addPart("secret", secretbody); DefaultHttpClient client= new DefaultHttpClient(); 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){ return resultStr; }else{ return ""; } } /** * 显示的调用HttpPost来使用post方式提交请求,并且将请求的参数提前拼装成json字符串, * 直接使用StringEntity来发送参数不必要再使用key:value的形式来设置请求参数, * 在服务端使用request.getInputStream()来把request中的发送过来的json字符串解析出来, * 就是因为使用StringEntity来包装了传递的参数 * @param httpUrl * @param jsonParam */ @SuppressWarnings({ "resource", "deprecation" }) publicstatic String httpPostShow(String httpUrl,String jsonParam){ httpUrl="http://192.168.199.138/weixin/test.php"; jsonParam="{\"appid\":\"appidbbbb33333\",\"secret\":\"secretaaaaa3333\"}";//拼装一个json串 DefaultHttpClient httpClient = new DefaultHttpClient(); //这里就是显示的调用post来设置使用post提交请求 HttpPost method = new HttpPost(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){ return result; }else{ return ""; } } privatestaticbyte[] getContent(HttpResponse response) throws IOException { InputStream result = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); try { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = resEntity.getContent(); int len = 0; while ((len = result.read()) != -1) { out.write(len); } return out.toByteArray(); } } catch (IOException e) { e.printStackTrace(); thrownew IOException("getContent异常", e); } finally { out.close(); if (result != null) { result.close(); } } returnnull; }
以上为java中http的get与post请求方式,httpPost(String httpUrl,byte[] imagebyte)这个方法可以传递图片等非结构化数据,以流的形式传递。
原文:http://www.cnblogs.com/xjh713/p/6075844.html
内容总结
以上是互联网集市为您收集整理的java-HttpGetPost-图片字节流上传全部内容,希望文章能够帮你解决java-HttpGetPost-图片字节流上传所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。