Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
未找到Spring MVC文件异常 - 源码之家

未找到Spring MVC文件异常

问题描述:

我正在使用Spring MVC。在我的控制,我从MyClass的未找到Spring MVC文件异常

调用的函数
MyClass{ 
    public static void readFile(){ 
    File file = new FileReader("myPath/myFile.txt"); 
    ... 
    } 
    public static void main(String[] args) { 
    readFile();//No problem 
    } 
} 

控制器:

@Controller 
public class MyController{ 
    @RequestMapping(value = "/url", method = RequestMethod.GET) 
    public String readFile(Locale locale, Model model) { 
    MyClass.readFile();//File not found exception 
    .... 
    } 
} 

读数的时间我的主要测试()MyClass的作品,但是当我在服务器和访问运行该项目“/ url”,我得到这个:

java.io.FileNotFoundException: myPath/myFile.txt (The system cannot find the path specified) 

如何在控制器中指定路径?

谢谢你的时间。

+1

也许这可能对你有用https://*.com/questions/36371748/spring-boot-access-static-resources-missing-scr-main-resources – lenach87

服务器上的工作目录是什么?在该目录中,服务器上是否存在文件的相对路径(“myPath/myFile.txt”)?

假设您只需要读取myFile.txt,那么通常myFile.txt将包含在WAR中,并通过getResourceAsStream进行读取。

我想你正在寻找在Spring中的Resource实现。 Spring将决定使用哪种类型的资源,例如,如果你的文件在类路径中,Spring将创建ClasspathResource,或者它指向一个文件,它将创建一个FileResource。如果您在使用Spring 4.尝试像

@Value("file:///myPath/myFile.txt") 
Resource myFile; 

看一看这个Spring Resources文档

,因为这不是绝对路径。计划会发现在工作目录文件 您可以添加到第一个程序这一点,第二个

System.out.println("Working Directory = " + 
    System.getProperty("user.dir")); 

你会看到差别。