Upload image + parameters Httpurlconnection

Asked

Viewed 420 times

0

I have a Rest server and I need to upload images and also send parameters. I’ve done a lot of research, but I still can’t get the client to work. By the browser works right, via code I can send the image but the parameter does not. Follow server code - Jersey 1.17.

@POST
@Path("/query")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload( @FormDataParam("file") InputStream fileInputStream, 
                        @FormDataParam("file") FormDataContentDisposition contentHeader, 
                        @FormDataParam("codvendedor") String codVendedor)
{
    System.out.println("Rep"+codVendedor);
    String path = "C:\\arquivosXML\\"+contentHeader.getFileName();

    System.out.println("path "+path);

    saveFile(fileInputStream, path);

    String output = "File saved";
    return Response.status(Response.Status.OK).entity(output).build();
}

Follow the customer

public void post2() throws MalformedURLException, IOException
{
    String paramToSend = "4";
    File fileToUpload = new File("C:\\doc\\fachada2.png");
    String boundary = Long.toHexString(System.currentTimeMillis()); 
    String userCredentials = "rep4:44";

    String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes()));

    HttpURLConnection  connection = (HttpURLConnection) new URL(URL_UPLOAD  ).openConnection();
    connection.setDoOutput(true); // This sets request method to POST.

    connection.setRequestMethod("POST");
    connection.setRequestProperty ("Authorization", basicAuth);
    connection.setRequestProperty ("Connection", "keep-alive");
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);


    DataOutputStream  output = null;
    try
    {
        output = new DataOutputStream (connection.getOutputStream());
        output.writeBytes("--" + boundary+"\r\n");
        output.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"fachada2.png\""+"\r\n");
        output.writeBytes("Content-Type: image/png;"+"\r\n");
        output.writeBytes("\r\n");

        byte[] anexo = convertInputstreamToBytes(fileToUpload);
        output.write(anexo);

        output.writeBytes("--" + boundary+"\r\n");
        output.writeBytes("Content-Disposition: form-data; name=\"codvendedor\""+"\r\n");
        output.writeBytes("Content-Type: text/plain; charset=UTF-8"+"\r\n");
        output.writeBytes(paramToSend+"\r\n");

        output.writeBytes("--" + boundary + "--" + "\r\n");
    }
    finally
    {
        if (output != null)
        {
            output.flush();
            output.close();
        }
    }


    int responseCode = ((HttpURLConnection) connection).getResponseCode();
    System.out.println(responseCode); 
}
  • Looking at your request, I think you missed informing the field Content-Length, According to RFC, it is not mandatory, but is a standard adopted by some servers. You could also compare your request to the Browser request (using Developer Tools), just for verification. I think that answer might help you: http://stackoverflow.com/a/12119430/3404639.

  • With or without the content-length the result is the same, the file will, but the parameter is always null.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.