quocvuongdn

Transit from client to another server by POST multipart form

Sep 24th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. public String sendPostMultipart(HttpServletRequest request, InputStream uploadedInputStream, FormDataBodyPart body) {
  2.         try {
  3.             String fileName = body.getFormDataContentDisposition().getFileName();
  4.  
  5.             HttpPost httpPost = new HttpPost(ME_URL);
  6.             httpPost.setHeader("enctype", "multipart/form-data");
  7.  
  8.             MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  9.             builder.addBinaryBody("avatar", uploadedInputStream, ContentType.create(body.getMediaType().toString(), "UTF-8"), fileName);
  10.             Enumeration<String> parameterNames = request.getParameterNames();
  11.  
  12.             while (parameterNames.hasMoreElements()) {
  13.                 String paramName = parameterNames.nextElement();
  14.                 String paramValue = request.getParameter(paramName);
  15.                 builder.addTextBody(paramName, paramValue);
  16.             }
  17.             HttpEntity entity = builder.build();
  18.             httpPost.setEntity(entity);
  19.             CloseableHttpClient httpClient = HttpClients.createDefault();
  20.             CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
  21.             HttpEntity responseEntity = httpResponse.getEntity();
  22.             if (responseEntity != null) {
  23.                 json_result = EntityUtils.toString(responseEntity);
  24.             }
  25.         } catch (IOException ex) {
  26.             WriteLog("[IOException] - className:" + ProcessRequest.class.getName() + " --- exception:" + ex);
  27.         }
  28.         return json_result;
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment