簡體   English   中英

Spring FileNotFoundException ..(沒有此類文件或目錄)

[英]Spring FileNotFoundException .. (No such file or directory)

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

通過使用/WEB-INF/content/customermapping.xml的路徑,您將告訴Java將名為customermapping.xml的文件寫入驅動器目錄下 WEB-INF/content目錄。 這聽起來不像您想要的。

嘗試刪除前導/

但是,在托管Web應用程序的同一目錄中寫入文件可能不是一個好主意,因為諸如Tomcat之類的servlet容器在取消部署應用程序時使用默認設置只是刪除了整個目錄。 最好寫入Webapp外部的目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM