Search code examples
springrestapimaven-2spring-batch

Upload a csv in src / main / resources from a post method


I have set up a REST API which allows to launch a spring batch job from a POST method that has the CSV at the input of the batch but I do not know how to make it feed because I can not write the csv in the src / main / resources of my project maven, I know that it is here that it must be written because in production the files that are present there will go in webapp / classes / to be used.

    byte[] fileBytes = Base64.decodeBase64(cleanFile);
    Files.write(Paths.get(getClass().getClassLoader().getResource("/").toURI()), fileBytes);

Generate that :

"message": "status 500 reading SalesClient#uploadCsv(Base64FileDTO); content:\n{\"timestamp\":1507151307711,\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.io.FileNotFoundException\",\"message\":\"class path resource [bonjour.csv] cannot be resolved to URL because it does not exist\",\"path\":\"/sales/upload\"}",

Solution

  • You'll need to write the file to some form of staging location. When working locally, a temp directory usually works well for this.