使用httpClient传输ByteArrayEntity数据

来源:互联网 发布:轴承故障数据集 编辑:程序博客网 时间:2024/06/11 02:47

最近想通过https传输数据,客户端使用POST推送,因为数据一个结构体的集合,所以选用ByteArrayEntity形式发送。


---------------------------------------------------------------------------------------------------------

数据结构:

                Messages.Builder messageBuilder = CanalPacket.Messages.newBuilder();
                List<ByteString> list = new ArrayList<ByteString>();
                for (Entry entry : newEntryList) {
                list.add(entry.toByteString());
                    messageBuilder.addMessages(entry.toByteString());
                }



-----------------------------------------------------------------------------------------------------------

HttpClient Post实现:

public HttpResponse doPost(String url, byte[] datas, Map<String, String> formParams) throws URISyntaxException, ClientProtocolException, IOException{
HttpPost httpPost = new HttpPost();
URIBuilder builder = new URIBuilder(url);
httpPost.setURI(builder.build());
httpPost.setHeader("Accept-Charset", "GBK");
if (formParams!=null && !formParams.isEmpty()){
for(Map.Entry<String, String> entry:formParams.entrySet()){
httpPost.setHeader(entry.getKey(),entry.getValue());
}
}
if (datas != null) {
   //ByteArrayEntity arrayEntity = new ByteArrayEntity(voiceContent);
   httpPost.setEntity(new ByteArrayEntity(datas));
}
return client.execute(httpPost);
}

-----------------------------------------------------------------------------------------------------------

0 0