0

I have a Java HttpClient that executes the following code:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://exampleutl.com/upload/");

File file = new File("C:/src_path/binary.doc");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

builder.setMode(HttpMultipartMode.STRICT);

FileBody fileBody = new FileBody(file); //image should be a String
builder.addPart("file", fileBody);
post.setEntity(builder.build());

client.execute(post);

I cannot figure out what the server method mapped to the /upload/ path should look like.

The server that accepts this file upload request is Spring 4.0. Something like this:

@RequestMapping(method = RequestMethod.POST, value = "/upload/")
public @ResponseBody String saveUpload(UploadDto dto) throws IOException,ServletException {
    File file = new File("C:/dest_path/" + dto.getFile().getOriginalFilename());
    FileUtils.writeByteArrayToFile(file, dto.getFile().getBytes());
    return "success";
}

The above server method gets called by the client.execute() but the UploadDto is empty. Here is the UploadDto:

public class UploadDto {
    private MultipartFile file;

    public MultipartFile getFile() {
        return file;
    }

    public void setFile(MultipartFile file) {
        this.file = file;
    }
}

Any assistance would be greatly appreciated!

Vance Cagle
  • 123
  • 1
  • 9

2 Answers2

1

You seem to be missing a MultipartResolver bean from your Spring servlet context. Something like

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}

You're sending your request to

HttpPost post = new HttpPost("http://exampleutl.com/upload/");

Assuming your context path is ROOT, ie. empty, your handler method should be mapped to /upload.

@RequestMapping(method = RequestMethod.POST, value = "/upload")
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • You are right, thanks for noticing that mistake. Unfortunately this was no solution to the problem. I have edited the question to address this change and add a little more information. – Vance Cagle Apr 24 '14 at 22:41
  • 1
    @VanceCagle You seem to be missing a `MultipartResolver` bean from your context. Spring can't process the request properly without the `MultipartResolver`. – Sotirios Delimanolis Apr 24 '14 at 22:47
0

The ultimate answer was that the server method should look like this:

@RequestMapping(method = RequestMethod.POST, value = "/upload/")
public @ResponseBody String saveUpload(@RequestParam("file") final MultipartFile mpf) throws IOException, ServletException, FileUploadException {
    File file = new File("C:/dest_path/" + mpf.getOriginalFilename());
    FileUtils.writeByteArrayToFile(file, mpf.getBytes());

    return "success";
}

As mentioned by Sotirios Delimanois the MultipartResolver is indeed a required part of the solution as well:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
} 
Vance Cagle
  • 123
  • 1
  • 9