Multipart data sending got 408 Status

649 Views Asked by At

guys. Need help with sending POST request with multipart-data.

I have a method to create request on my client side. Here it is:

public void sendMultipart(String cmd , Employee emp) {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost uploadFile = new HttpPost(baseUrl + cmd);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    FileBody f = new FileBody(emp.getPhoto());
    try {
    StringBody s = new StringBody(emp.getLogin());
    builder.addPart("name", s);
    builder.addPart("file", f);
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(RestTemplateRequester.class.getName()).log(Level.SEVERE, null, ex);
    }
    uploadFile.setHeader("Accept", "application/json");
        uploadFile.setHeader("Content-type", "application/json");
        uploadFile.setHeader("enctype","multipart/form-data");
        uploadFile.setHeader("accept-charset","UTF-8");
    //builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);
    //builder.addBinaryBody(emp.getLogin(), emp.getPhoto(), ContentType.MULTIPART_FORM_DATA, "file");
    HttpEntity multipart = builder.build();

    uploadFile.setEntity(multipart);

    try {
        HttpResponse httpResponse =  httpClient.execute(uploadFile);

        int status = httpResponse.getStatusLine().getStatusCode();
        String str = httpResponse.getStatusLine().getReasonPhrase();
    } catch (IOException ex) {
        Logger.getLogger(RestTemplateRequester.class.getName()).log(Level.SEVERE, null, ex);
    }

}

Also I have a method to handle request on my server side:

@RequestMapping(value = "photo", consumes = "multipart/form-data")
public @ResponseBody
void uploadFileHandler(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Create the file on server
            File serverFile = new File(name);
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

        } catch (Exception e) {
             e.getMessage();
        }
    }
}

And in my context:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

     <!-- setting maximum upload size -->
    <property name="maxUploadSize" value="100000" />

</bean>

The problem which I have now - is a 408 error "Request Time Out". My common aim is - send JSON with file to server. I so green with web services in java, so i got some troubles. Please, It will be great, if someone can give me a little advice. Thank you.

1

There are 1 best solutions below

0
On

What you are trying to do is not an accepted way to upload file (Embedding file inside of JSON). And furthermore your Upload file handler is not suited to handle JSON instead in can handle only a multipart file and a name as parameter. Please look at my previous q/a on how to successfully upload a file using Spring MVC Multipart and RestTemplate.