Java 从 Spring Controller 获取 Web App root

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2066843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:10:25  来源:igfitidea点击:

Get Web App root from Spring Controller

javaspringfile-io

提问by D.C.

I am trying to write an uploaded multipart file to the filesystem. I have a directory called audio which sits in the root of my web application (not inside WEB-INF, but beside it, to it's publicly accessible like css and javascript).

我正在尝试将上传的多部分文件写入文件系统。我有一个名为 audio 的目录,它位于我的 Web 应用程序的根目录中(不在 WEB-INF 内,但在它旁边,它可以像 css 和 javascript 一样公开访问)。

I want to write the uploaded file to that directory but I can't seem to get the path I need. I thought getting a ServletContext() then using realPath() may work, but I don't have a reference to ServletContext through a Spring controller. Thanks for any hep

我想将上传的文件写入该目录,但似乎无法获得所需的路径。我认为获取 ServletContext() 然后使用 realPath() 可能会起作用,但我没有通过 Spring 控制器引用 ServletContext。感谢您的帮助

@RequestMapping(value="/uploadSample")
public ModelAndView upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile f) {

    if (f == null) {
        return new ModelAndView("upload", "msg", "The file is null.");
    }
    try {
        // I need to set AUDIO_PATH to <webAppRoot>/audio
        FileOutputStream file = new FileOutputStream(AUDIO_PATH + "/" + f.getOriginalFilename());
        file.write(f.getBytes());
        file.close();
    }
    catch (FileNotFoundException ex) {
        Logger.getLogger(SampleUploadController.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
            Logger.getLogger(SampleUploadController.class.getName()).log(Level.SEVERE, null, ex);
    }



   return new ModelAndView("upload", "msg", "File ( " + f.getOriginalFilename() + ") successfully uploaded.");
}

}

}

采纳答案by Kevin

I thought getting a ServletContext() then using realPath() may work, but I don't have a reference to ServletContext

我认为获取 ServletContext() 然后使用 realPath() 可能会起作用,但我没有对 ServletContext 的引用

Yes you do. See HttpServletRequest.getSession().getServletContext()

是的你是。参见HttpServletRequest.getSession().getServletContext()

回答by axtavt

To get reference to ServletContext, your class can implement ServletContextAware

要获得对 的引用ServletContext,您的类可以实现ServletContextAware

ServletContextis also accessible in the web application container under the bean name servletContext, so you can inject it like any other bean in Spring. This works even if you don't have a session, and dependency injection is the Spring way.

ServletContext也可以在 web 应用程序容器中以 bean name 访问servletContext,因此您可以像 Spring 中的任何其他 bean 一样注入它。即使您没有会话,这也有效,并且依赖注入是 Spring 的方式。

回答by luke

Getting server url using ServletContextis not safe across different environments.

ServletContext在不同环境中使用获取服务器 url是不安全的。

It would be better to retrieve the url from a property file.

从属性文件中检索 url 会更好。