Java SpringMVC如何从控制器访问静态资源
好的,我正在使用SpringMVC4.0,从控制器读取Java SpringMVC如何从控制器访问静态资源,java,spring,spring-mvc,Java,Spring,Spring Mvc,好的,我正在使用SpringMVC4.0,从控制器读取txt文件时遇到问题 @RequestMapping("/file") public class FileController { @RequestMapping(method=RequestMethod.GET) public String getFile() throws IOException{ BufferedReader br = new BufferedReader(new FileReader(
txt
文件时遇到问题
@RequestMapping("/file")
public class FileController {
@RequestMapping(method=RequestMethod.GET)
public String getFile() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("docs/file.txt"));
StringBuilder sb = new StringBuilder();
try {
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
} finally {
br.close();
}
return sb.toString();
}
}
我在调度程序servlet中设置了
<mvc:resources mapping="/docs/**" location="/docs/"/>
我已经尝试了FileReader(path)的所有路径,但无法获取该文件。。。我怎么做
我的目录结构是:
Application
---WepPages
-------META-INF
-------WEB-INF
-------docs
---SourcePackages
---Libraries
.
.
.
.
.
这些资源通常被打包成战争。这就是为什么在文件系统中找不到它们。尽管您仍然可以使用classloader访问它们:
getClass().getResourceAsStream("/docs/file.txt")
Spring可以使用以下接口访问底层资源:
@Value("file:/docs/file.txt")
private Resource myFile;
@RequestMapping(method = RequestMethod.GET)
public String getFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(myFile.getFile()));
// do something
}
您可以使用
ServletContext
读取文件。e、 g
ServletContext context = //...
InputStream is = context.getResourceAsStream("/docs/file.txt");
另外,请选中此项-。我需要这样做,以便为我的视图聚合js和css文件列表。
文件路径可以传递到视图,因此不需要手动注册。
我就是这样做的-
@Controller
public class HomeController {
WebApplicationContext webappContext;
List<String> jsFiles = new ArrayList<>();
List<String> cssFiles = new ArrayList<>();
@Autowired
public HomeController(ServletContext servletContext) throws IOException{
webappContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
Resource[] jsResources = webappContext.getResources("content/modules/**/*.js");
Resource[] cssResources = webappContext.getResources("content/modules/**/*.css");
for (Resource resource : jsResources) {
jsFiles.add(resource.getURL().getPath());
}
for (Resource resource : cssResources) {
cssFiles.add(resource.getURL().getPath());
}
}
@RequestMapping({ "/", "/home**" })
public ModelAndView getHomePage() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("home");
modelAndView.addObject("jsFiles", jsFiles);
modelAndView.addObject("cssFiles", cssFiles);
return modelAndView;
}
}
@控制器
公共类家庭控制器{
WebApplicationContext-webappContext;
List jsFiles=new ArrayList();
List cssFiles=new ArrayList();
@自动连线
公共HomeController(ServletContext ServletContext)引发IOException{
webappContext=WebApplicationContextILS.getRequiredWebApplicationContext(servletContext);
Resource[]jsResources=webappContext.getResources(“content/modules/***.js”);
Resource[]cssResources=webappContext.getResources(“content/modules/***.css”);
for(资源:jsResources){
add(resource.getURL().getPath());
}
for(资源:cssResources){
添加(resource.getURL().getPath());
}
}
@请求映射({”/“,“/home**”})
公共模型和视图获取主页(){
ModelAndView ModelAndView=新建ModelAndView();
modelAndView.setViewName(“主页”);
addObject(“jsFiles”,jsFiles);
添加对象(“cssFiles”,cssFiles);
返回模型和视图;
}
}
您混淆了“资源”的定义。静态资源由SpringMVC自动处理,不需要专用控制器。