@Test public void uploadFile() throws UnsupportedEncodingException { UploadFileRequest request = new UploadFileRequest("/user/uploadFile"); request.setMethod(MethodTypeEnum.POST); request.setHttpContentType(ContentType.MULTIPART_FORM_DATA); UploadFileRequest.UploadFileBody body = new UploadFileRequest.UploadFileBody(); body.setFile(new File("D:\\workspace\\idea\\individual\\springboot-learn\\springboot-http-client\\src\\main\\resources\\static\\images.jpg")); request.setBody(body); String result = HttpUtil.uploadFile(request.composeUrl(baseUrl, request.getQueryParameters()), request.getBody()); AbstractResponse response = JacksonUtil.readValue(result, request.getResponseClass()); log.info(response.toString()); }
public static String uploadFile(String requestUrl, Object data) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(DEFAULT_ENCODING)); ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), DEFAULT_ENCODING); HttpPost post = new HttpPost(requestUrl); if (data != null) { if (data instanceof File) { FileBody fileBody = new FileBody((File) data); multipartEntityBuilder.addPart("file", fileBody); } else { Field[] fields = data.getClass().getDeclaredFields(); try { for (Field field : fields) { field.setAccessible(true); Object fieldValue = field.get(data); if (fieldValue != null) { if (File.class.equals(field.getType())) { FileBody fileBody = new FileBody((File) field.get(data)); multipartEntityBuilder.addPart(field.getName(), fileBody); } else { StringBody stringBody = new StringBody(field.get(data).toString(), contentType); multipartEntityBuilder.addPart(field.getName(), stringBody); } } } } catch (IllegalAccessException e) { logger.error("Upload file IllegalAccessException -> " + e); throw new RuntimeException("Upload file IllegalAccessException"); } } HttpEntity reqEntity = multipartEntityBuilder.build(); post.setEntity(reqEntity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, Charset.forName(DEFAULT_ENCODING)); }
HttpUtil { public static String uploadFile(String requestUrl, Object data) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(DEFAULT_ENCODING)); ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), DEFAULT_ENCODING); HttpPost post = new HttpPost(requestUrl); if (data != null) { if (data instanceof File) { FileBody fileBody = new FileBody((File) data); multipartEntityBuilder.addPart("file", fileBody); } else { Field[] fields = data.getClass().getDeclaredFields(); try { for (Field field : fields) { field.setAccessible(true); Object fieldValue = field.get(data); if (fieldValue != null) { if (File.class.equals(field.getType())) { FileBody fileBody = new FileBody((File) field.get(data)); multipartEntityBuilder.addPart(field.getName(), fileBody); } else { StringBody stringBody = new StringBody(field.get(data).toString(), contentType); multipartEntityBuilder.addPart(field.getName(), stringBody); } } } } catch (IllegalAccessException e) { logger.error("Upload file IllegalAccessException -> " + e); throw new RuntimeException("Upload file IllegalAccessException"); } } HttpEntity reqEntity = multipartEntityBuilder.build(); post.setEntity(reqEntity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, Charset.forName(DEFAULT_ENCODING)); } }
HttpUtil { public static String uploadFile(String requestUrl, Object data) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(DEFAULT_ENCODING)); ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), DEFAULT_ENCODING); HttpPost post = new HttpPost(requestUrl); if (data != null) { if (data instanceof File) { FileBody fileBody = new FileBody((File) data); multipartEntityBuilder.addPart("file", fileBody); } else { Field[] fields = data.getClass().getDeclaredFields(); try { for (Field field : fields) { field.setAccessible(true); Object fieldValue = field.get(data); if (fieldValue != null) { if (File.class.equals(field.getType())) { FileBody fileBody = new FileBody((File) field.get(data)); multipartEntityBuilder.addPart(field.getName(), fileBody); } else { StringBody stringBody = new StringBody(field.get(data).toString(), contentType); multipartEntityBuilder.addPart(field.getName(), stringBody); } } } } catch (IllegalAccessException e) { logger.error("Upload file IllegalAccessException -> " + e); throw new RuntimeException("Upload file IllegalAccessException"); } } HttpEntity reqEntity = multipartEntityBuilder.build(); post.setEntity(reqEntity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, Charset.forName(DEFAULT_ENCODING)); }  }
HttpUtil { public static String uploadFile(String requestUrl, Object data) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(DEFAULT_ENCODING)); ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), DEFAULT_ENCODING); HttpPost post = new HttpPost(requestUrl); if (data != null) { if (data instanceof File) { FileBody fileBody = new FileBody((File) data); multipartEntityBuilder.addPart("file", fileBody); } else { Field[] fields = data.getClass().getDeclaredFields(); try { for (Field field : fields) { field.setAccessible(true); Object fieldValue = field.get(data); if (fieldValue != null) { if (File.class.equals(field.getType())) { FileBody fileBody = new FileBody((File) field.get(data)); multipartEntityBuilder.addPart(field.getName(), fileBody); } else { StringBody stringBody = new StringBody(field.get(data).toString(), contentType); multipartEntityBuilder.addPart(field.getName(), stringBody); } } } } catch (IllegalAccessException e) { logger.error("Upload file IllegalAccessException -> " + e); throw new RuntimeException("Upload file IllegalAccessException"); } } HttpEntity reqEntity = multipartEntityBuilder.build(); post.setEntity(reqEntity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, Charset.forName(DEFAULT_ENCODING)); }  static String get(String requestUrl); static String post(String requestUrl, Object data, ContentType contentType); static String uploadFile(String requestUrl, Object data); }
HttpUtil { public static String uploadFile(String requestUrl, Object data) { if (StringUtils.isBlank(requestUrl)) { throw new RuntimeException("requestUrl cann't be null"); } MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(DEFAULT_ENCODING)); ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), DEFAULT_ENCODING); HttpPost post = new HttpPost(requestUrl); if (data != null) { if (data instanceof File) { FileBody fileBody = new FileBody((File) data); multipartEntityBuilder.addPart("file", fileBody); } else { Field[] fields = data.getClass().getDeclaredFields(); try { for (Field field : fields) { field.setAccessible(true); Object fieldValue = field.get(data); if (fieldValue != null) { if (File.class.equals(field.getType())) { FileBody fileBody = new FileBody((File) field.get(data)); multipartEntityBuilder.addPart(field.getName(), fileBody); } else { StringBody stringBody = new StringBody(field.get(data).toString(), contentType); multipartEntityBuilder.addPart(field.getName(), stringBody); } } } } catch (IllegalAccessException e) { logger.error("Upload file IllegalAccessException -> " + e); throw new RuntimeException("Upload file IllegalAccessException"); } } HttpEntity reqEntity = multipartEntityBuilder.build(); post.setEntity(reqEntity); } logger.info("HttpClient request uri -> " + requestUrl); logger.info("HttpClient request body -> " + data); return executeRequest(post, Charset.forName(DEFAULT_ENCODING)); }  static String get(String requestUrl); static String post(String requestUrl, Object data, ContentType contentType); static String uploadFile(String requestUrl, Object data);  }