我是 Spring MVC 的新手。我创建了一个新的 spring 控制器方法,它接受一个 fileoutputstream 并将 xml 写入它,如下所示:
@RequestMapping(value = "/xml", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
public final void getMappingsAsXML(HttpServletResponse response) {
Customer customer = getCustomerMappings();
try {
// Generates xml file
xmlHelper.save(customer, new StreamResult(new FileOutputStream("/WEB-INF/content/customermapping.xml")));
// print to browser
// read generated file and write to response outputsteam
} catch (XmlMappingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但是,上面的代码会引发以下异常:
java.io.FileNotFoundException: /WEB-INF/content/customermapping.xml (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:84)
内容目录已存在于 WEB-INF 文件夹中。另外,有没有更好的方法在春季将文件写入浏览器?
[顺便说一句,我正在使用 Maven。]