Spring FileNotFoundException ..(没有这样的文件或目录)

时间:2012-08-28 18:18:15

标签: java spring spring-mvc

我是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。]

1 个答案:

答案 0 :(得分:0)

通过使用/WEB-INF/content/customermapping.xml路径,您告诉Java将名为customermapping.xml的文件写入驱动器的WEB-INF/content目录。这听起来不像你想要的那样。

尝试删除前导/

然而,写入托管Web应用程序的同一目录中的文件可能是一个坏主意,因为像Tomcat这样的一些servlet容器只需在取消部署应用程序时删除整个目录(使用默认设置)。写入webapp外部的目录要好得多。