Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Java rest service client send file and data


I have a rest service which let me post file and data, it works fine like this (Restlet Client in Chrome complement):

but I've tried this in java:

public static void consultaPost2f() {

try {
File file = new File("/home/teno/Documentos/test.pdf");
String url="";

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("codEmpresa", "204");
builder.addTextBody("nroIngreso", "123");
builder.addTextBody("codDocumento", "105");
builder.addTextBody("fechaVencimiento", "2018-02-28");
//builder.addTextBody("archivoCargado", "test.pdf");
//builder.addBinaryBody("archivoCargado", file,ContentType.create("application/pdf"), "test.pdf");
builder.addBinaryBody("archivoCargado", file,ContentType.MULTIPART_FORM_DATA, "test.pdf");
HttpEntity multipart =;

CloseableHttpResponse response = client.execute(httpPost);

if (response != null) {
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// Read the response string if required
InputStream responseStream = responseEntity.getContent() ;
if (responseStream != null){
BufferedReader br = new BufferedReader (new InputStreamReader (responseStream)) ;
String responseLine = br.readLine() ;
String tempResponseString = "" ;
while (responseLine != null){
tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator") ;
responseLine = br.readLine() ;
br.close() ;
if (tempResponseString.length() > 0){



} catch (MalformedURLException e) {


} catch (IOException e) {



and it gives me this error message:

Apache Tomcat/8.0.32 (Ubuntu) - Informe de ErrorH1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;} {color : black;}.line {height: 1px; background-color: #525D76; border: none;}

Estado HTTP 400 - Bad Request

type Informe de estado

mensaje Bad Request

descripción El requerimiento enviado por el cliente era sintácticamente incorrecto.

Apache Tomcat/8.0.32 (Ubuntu)

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Java rest service client send file and data


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription