URI location = new URI(request.getLocationUri());
            HttpRequestBase req = null;
            String responseBody = "";

            req = new HttpPost(location);
            MultipartEntity reqEntity = new MultipartEntity();
            for (Map.Entry<String, byte[]> file : uploadFile.entrySet()) {
                reqEntity.addPart(file.getKey(),
                        new ByteArrayBody(file.getValue(), "re-signup.PNG"));
            }
            for (Map.Entry<String, Object> param : parameters.entrySet()) {
                reqEntity.addPart(param.getKey(), new StringBody((String) param.getValue()));
            }
            ((HttpPost) req).setEntity(reqEntity);
            // TODO 千万不要加这个,加了就拉稀了
//            req.setHeader(OAuth.HeaderType.CONTENT_TYPE, OAuth.ContentType.UPLOAD_FILE);

            HttpResponse response = client.execute(req);
            Header contentTypeHeader = null;
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseBody = EntityUtils.toString(entity, CommonParameters.UTF8);
                contentTypeHeader = entity.getContentType();
            }
            String contentType = null;
            if (contentTypeHeader != null) {
                contentType = contentTypeHeader.toString();
            }

注意:千万不要加req.setHeader,根据接口需要参数类型不同,可构造不同的ContentBody;

中文乱码问题:

StringBody stringBody = new StringBody((String) param.getValue(),
                        Charset.forName(CommonParameters.UTF8));
                reqEntity.addPart(param.getKey(), stringBody);

 

相关文章:

  • 2021-07-05
  • 2021-04-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-08-12
  • 2022-01-08
  • 2022-03-03
  • 2022-12-23
相关资源
相似解决方案