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!