Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a static file in a Spring Controller

I'm pretty new to Spring, so apologies if I don't see the obvious answer here.

I set up small demo project with a Spring MVC controller and deployed it to App Engine. In my controller I would like to read the content of a static file into a String. What's the best way of doing that?

I googled a bit but I'm probably searching for the wrong thing. I tried the below, but it does not work:

@Controller
@RequestMapping("/myController")
public class MyController {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public @ResponseBody String myTest() {

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        String content = "";
        try {
            fileReader = new FileReader("file:/WEB-INF/content/somecontent.txt");
            bufferedReader = new BufferedReader(fileReader);
            content = bufferedReader.readLine();
            bufferedReader.close(); 
        }
        catch (Exception ignored) { 
            // ignore
        }
        return content;
    }
}

Any push into the right direction will be highly appreciated :-)

like image 483
StefanS Avatar asked Feb 28 '11 19:02

StefanS


People also ask

How static files are served in Spring?

Using Spring BootSpring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

Where do static files go in Spring boot?

The file is located in the src/main/resources/static directory, which is a default directory where Spring looks for static content. In the link tag we refer to the main. css static resource, which is located in the src/main/resources/static/css directory. In the main.

How do I upload a file to Spring boot?

Spring Boot file uploader Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and. Send a response code to the client indicating the Spring file upload was successful.


2 Answers

Servlet containers in general, and GAE in particular, won't let you use the File API from within the servlet container like that.

Instead, autowire your controller with the ServletContext, and fetch the resource from that. Also, your exception handling isn't great, you shouldn't ignore exceptions like that, they're there for a reason.

Something like should be OK:

@Controller
@RequestMapping("/myController")
public class MyController {

    private @Autowired ServletContext servletContext;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public @ResponseBody String myTest() throws IOException {
        InputStream inputStream = null;
        try {
            inputStream = servletContext.getResourceAsStream("/WEB-INF/content/somecontent.txt");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            return bufferedReader.readLine();
        } finally {
            if (inputStream != null) {
               inputStream.close();
            }
        }
    }
}

Alternatively, if you're using Spring 3.x, this might be even simpler:

<mvc:view-controller path="/test" view-name="/WEB-INF/content/somecontent.txt"/>

See docs for what this does, but it may mean you can avoid any code.

like image 74
skaffman Avatar answered Oct 17 '22 21:10

skaffman


The notation "file:" and "classpath:" isn't right with FileReader. I suggest you to create a FileSystemResource

FileSystemResource resource = new FileSystemResource("/WEB-INF/content/somecontent.txt");

and then to use getFile() or getInputStream() to read file. This is very useful in a web application, because you can use relative path.

like image 36
javanna Avatar answered Oct 17 '22 20:10

javanna