2015-12-05 283 views
2

我已經下載該文件下面的代碼: -如何在Spring中使用FileSystemResource正確下載文件?

@Controller 
public class FileController { 
    @RequestMapping(value = "/files/{file_name:.+}", method = RequestMethod.GET) 
    @ResponseBody 
    public FileSystemResource getFile(@PathVariable("file_name") String fileName) { 

     return new FileSystemResource("C:/Users/sourav/fileServer/"+fileName); 

    } 

} 

當我去的鏈接,第一次沒有任何顯示。當我重新加載只是一個文本文件名f.txt代替下載的pdf文件。我想要在瀏覽器中顯示PDF文件。如何解決這個問題呢 ?

+0

我有同樣的問題,使用Spring 4.3.4 – yglodt

回答

0

你的代碼沒問題。我認爲如果你使用pdf文件,它會按照你的預期工作,它會在瀏覽器中顯示。我測試了它,並在Chrome和Firefox中運行良好。可能是您的測試文件已損壞。

相關問題