【问题标题】:Spring MVC how to access Static Resource from ControllerSpring MVC 如何从 Controller 访问静态资源
【发布时间】:2014-11-20 18:38:33
【问题描述】:

好的,我正在使用 Spring MVC 4.0,但在从 Controller 读取 txt 文件时遇到问题。

我在 dispatcher-servlet 中设置

<mvc:resources mapping="/docs/**" location="/docs/"/>

所以在文档中我设置了file.txt,我想从控制器读取该文件。

@RequestMapping("/file")
public class FileController {

    @RequestMapping(method=RequestMethod.GET)
    public String getFile() throws IOException{
        BufferedReader br = new BufferedReader(new FileReader("docs/file.txt")); 
        StringBuilder sb = new StringBuilder();
        try {

        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
        }
        } finally {
            br.close();
        }
        return  sb.toString();
    }

}

我已经尝试了 FileReader(path) 的所有路径,但我无法获取该文件...我该怎么做?

我的目录结构是:

Application
---WepPages
-------META-INF
-------WEB-INF
-------docs

---SourcePackages
---Libraries
.
.
.
.
.

【问题讨论】:

  • 您正在混淆“资源”的定义。静态资源由 Spring MVC 自动处理,不需要专用控制器。

标签: java spring spring-mvc


【解决方案1】:

我需要这样做来为我的视图聚合 js 和 css 文件列表。
文件路径可以传递给视图,因此不需要手动注册。
我就是这样做的——

@Controller
public class HomeController {

WebApplicationContext webappContext;

List<String> jsFiles = new ArrayList<>();
List<String> cssFiles = new ArrayList<>();

@Autowired
public HomeController(ServletContext servletContext) throws IOException{

    webappContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);


    Resource[] jsResources = webappContext.getResources("content/modules/**/*.js");
    Resource[] cssResources = webappContext.getResources("content/modules/**/*.css");

    for (Resource resource : jsResources) {
        jsFiles.add(resource.getURL().getPath());
    }
    for (Resource resource : cssResources) {
        cssFiles.add(resource.getURL().getPath());
    }
}
@RequestMapping({ "/", "/home**" })
public ModelAndView getHomePage() {

    ModelAndView modelAndView = new ModelAndView();

    modelAndView.setViewName("home");
    modelAndView.addObject("jsFiles", jsFiles);
    modelAndView.addObject("cssFiles", cssFiles);

    return modelAndView;
}
}  

【讨论】:

    【解决方案2】:

    您可以使用ServletContext 读取文件。例如

    ServletContext context = //...
    InputStream is = context.getResourceAsStream("/docs/file.txt");
    

    另外,检查一下 - ServletContext and Spring MVC

    【讨论】:

      【解决方案3】:

      Spring可以通过Resource接口访问底层资源:

      @Value("file:/docs/file.txt")
      private Resource myFile;
      
      @RequestMapping(method = RequestMethod.GET)
      public String getFile() throws IOException {
      
      BufferedReader br = new BufferedReader(new FileReader(myFile.getFile()));
          // do something
      }
      

      【讨论】:

        【解决方案4】:

        资源通常被打包用于战争。这就是为什么您无法在文件系统中找到它们的原因。虽然您仍然可以使用类加载器访问它们:

        getClass().getResourceAsStream("/docs/file.txt")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-18
          • 2013-07-27
          • 2023-04-01
          • 1970-01-01
          • 2015-04-12
          • 2012-08-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多