0

I have a service (Spring Boot). One of my features is to read a docx file, replace some text and save on the clients computer.

I have a file skom.docx and locally I can without any problems find that file:

@Getter
public enum ReportTemplatesEnum {

    KANC("src/main/resources/templates/skom.docx");

    private final String path;
}

(...)
    File file = new File(reportData.getReportType().getPath()); --> the path from Enum
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            XWPFDocument doc = new XWPFDocument(fis);
(...)

But when I deploy my service to the server (containerized in Docker) I get the error: Request: /odo/incident/generateOneReport/255 methodGET

java.io.FileNotFoundException: /src/main/resources/templates/skom.docx (No such file or directory)

How to prepare the path to my files? Locally works perfectly.

UPDATE - SOLUTION

Resource resource = new ClassPathResource(reportData.getReportType().getPath());
                InputStream dbAsStream = resource.getInputStream();
                XWPFDocument doc = new XWPFDocument(dbAsStream);
Matley
  • 1,953
  • 4
  • 35
  • 73
  • 1
    Possible duplicate of [Spring Boot - Reading Text File using ResourceLoader](https://stackoverflow.com/questions/41754712/spring-boot-reading-text-file-using-resourceloader) – Anil Sep 23 '19 at 09:29
  • 1
    Yes, you're RIGHT! Solution: Resource resource = new ClassPathResource(reportData.getReportType().getPath()); InputStream dbAsStream = resource.getInputStream(); XWPFDocument doc = new XWPFDocument(dbAsStream); – Matley Sep 23 '19 at 20:05

0 Answers0