首页 > 代码库 > java-HttpGetPost-图片字节流上传
java-HttpGetPost-图片字节流上传
在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") public static 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" }) public static 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" }) public static 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 ""; } } private static byte[] 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(); throw new IOException("getContent异常", e); } finally { out.close(); if (result != null) { result.close(); } } return null; }
以上为java中http的get与post请求方式,httpPost(String httpUrl,byte[] imagebyte)这个方法可以传递图片等非结构化数据,以流的形式传递。
java-HttpGetPost-图片字节流上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。