jeudi 26 mars 2015

Spring RestTemplate POST results in 400 Bad REQUEST with .jpg files

I'm trying to upload a file to a PHP server (whose max filesize is much above what i'm trying to send). It works with some filetypes (e.g. png, jpeg), and when I'm using Advanced Rest Client for Chrome, but doesn't upload .jpg files with the Spring Boot app.


The MultiPartFile comes from a controller's method's @RequestParam MultipartFile file parameter.


Service code:



public String uploadFile(MultipartFile multipartFile) throws IOException {
String tempFile = "/tmp/"+multipartFile.getOriginalFilename().replaceAll("jpg","jpeg");

FileOutputStream fo = new FileOutputStream(tempFile);
fo.write(multipartFile.getBytes());
fo.close();

MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("file", new FileSystemResource(tempFile));
Message response = new RestTemplate().postForObject(uploaderUrl+"uploader.php", parts, Message.class);

//noinspection ResultOfMethodCallIgnored
new File(tempFile).delete();
return uploaderUrl+response.getResult();
}


Message.java:



public class Message<T>{
private T result;
//getter, setter omitted
}

Aucun commentaire:

Enregistrer un commentaire