在Spring中从Controller返回文件


问题内容

我是春天的新手。我有一个带有几个GET参数的RequestMapping的控制器。他们返回一个String。但是一种方法需要返回“ / res
/”文件夹中的文件。我怎么做?

@RequestMapping(method = RequestMethod.GET,value = "/getfile")
public @ResponseBody 
String getReviewedFile(@RequestParam("fileName") String fileName)
{
    return //the File Content or better the file itself
}

谢谢


问题答案:

@RequestMapping(value = “/files/{file_name}”, method = RequestMethod.GET)
@ResponseBody
public FileSystemResource getFile(@PathVariable(“file_name”) String fileName) {
return new FileSystemResource(myService.getFileFor(fileName));
}