[ad_1]
Hello I am building an application in Spring Boot and Thymeleaf and I have an upload page where I have two fields and an upload button. I complete the fields and I select and xml and send it to the api and the answer I receive back is that the file is not valid. The response is written by the api creator so probably what I am thinking I am not reading the file right before sending.
Here is my code:
Service:
public String upload(String standard, String cif, MultipartFile file) throws
IOException {
String url = "http://100.000:222/test/upload?standard="+standard+"&cif="+cif;
String content = file.toString();
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
HttpHeaders headers = new HttpHeaders();
headers.add("serial", "123");
headers.add("Content-Disposition", "text/xml");
headers.add("fisier", text);
RestTemplate restTemplate = new RestTemplate();
RequestEntity<Void> request = (RequestEntity<Void>) RequestEntity.get(URI.create(url))
.headers(headers)
.build();
String response = restTemplate.postForObject(url, request, String.class);
return response;
}
Here is the controller:
@PostMapping("/upload")
public String postUpload(@RequestParam("standard") String standard, @RequestParam("select") String select, @RequestParam("fisier") MultipartFile file, Model model) throws IOException {
String index = service.upload(standard, select, file);
System.out.println(index);
model.addAttribute("index", index);
return "upload";
}
This is the message I am getting back:
<header xmlns = "xxx: xxx: xxx: xx: respUploadFile: v1" dateResponse = "202205311034" ExecutionStatus = "1">
<Errors errorMessage = "The transmitted file is invalid. Org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog." />
</header>
Can someone tell me what I am doing wrong or give me an working example. Thanks
[ad_2]