2016-07-28 2 views
0

스프링 MVC를 사용하고 있습니다. 내 컨트롤러에서 나는 스프링 MVC 파일을 찾을 수 없습니다. 예외

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

MyClass에

에서 함수를 호출 컨트롤러 :

@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://stackoverflow.com/questions/36371748/spring-boot-access-static-resources-missing-scr-main-resources – lenach87

답변

0

서버의 작업 디렉토리 란 무엇입니까? 해당 디렉토리에서 파일 ("myPath/myFile.txt")에 대한 상대 경로가 서버에 있습니까?

myFile.txt 만 읽으면됩니다. 일반적으로 myFile.txt는 WAR 내에 포함되고 getResourceAsStream을 통해 읽습니다.

0

나는 스프링에서 Resource 구현을 찾고 있다고 생각합니다. Spring은 사용할 리소스 유형을 결정합니다. 예를 들어 파일이 클래스 경로에있는 경우 Spring은 ClasspathResource을 만들거나 FileResource을 만들 파일을 가리 킵니다. 당신이 봄을

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

같은 4 봅니다 뭔가를 사용하는 경우이 절대적 경로가 아니기 때문에이 Spring Resources 문서

0

에서보세요. 프로그램은 처음 프로그램에이를 추가 할 수 있습니다 작업 디렉토리 아래의 파일과 두 번째

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

당신은 차이를 볼을 찾을 수 있습니다.

관련 문제