D
D
DDwrt1002019-09-30 14:33:40
Java
DDwrt100, 2019-09-30 14:33:40

Why does SpringBoot lose its file folder when building?

Good afternoon, please tell me why is this happening?
There is a controller which should give files on request.
the folder with files is located along the path: ~\src\main\resources\webapp\WEB-INF

@Controller
@RequestMapping("/jr")
public class JReportFileController {
  @RequestMapping("/reportFile/{fileName}")
  public void downloadReportFile(HttpServletRequest request, HttpServletResponse response,
      @PathVariable("fileName") String fileName) {
    String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/reports/");
    Path file = Paths.get(dataDirectory, fileName);
    if (Files.exists(file)) {
      response.setContentType("application/jasper");
      response.addHeader("Content-Dispositions", "attachment;filename=" + fileName);
      try {
        Files.copy(file, response.getOutputStream());
        response.getOutputStream().flush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

If I run the code from the IDE everything works, the file is returned. However, after building and running the jar, the program returns nothing except code 200, the response body is empty. Where is the mistake ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yerlan Ibraev, 2019-09-30
@mad_nazgul

Because you need to build war, not jar :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question