获取项目文件夹的路径

时间:2015-11-11 21:30:53

标签: java tomcat bootstrap-table to-json

在使用netbeans构建Web应用程序时,我在某种程度上将arrayList提取为json格式。

public void convertToJSON(ArrayList list){

     ObjectMapper mapper = new ObjectMapper();

     try{
          mapper.writeValue(new File("C:\\temp\\data.json"), list);
        }catch(JsonGenerationException e){
          e.printStackTrace();
        }catch(JsonMappingException e){
          e.printStackTrace();
        }catch (IOException e){
          e.printStackTrace();
        }
}

,这会创建文件C:\temp\data.json。如何在JSPS存在的项目文件夹中创建此文件?我尝试用

确定位置
String dir = System.getProperty("user.dir");

但这会在C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.15\bin中创建文件。我想在projects文件夹中创建文件,以便用它在bootstrap table中显示我的数据。在那里,我通过

定义我的数据的位置
data-url="data.json"

我试过这个data-url="C:\temp\data.json",但我收到了错误

XMLHttpRequest cannot load file:///C://temp//data.json?order=desc&limit=10&offset=0. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

2 个答案:

答案 0 :(得分:1)

这是跨域问题,bootstrap表不支持file:// url,请使用Web服务器运行代码。以下问题可以帮助您:https://github.com/wenzhixin/bootstrap-table/issues/230

答案 1 :(得分:0)

要在项目文件夹中获取文件,您可以使用

    URL resourceUrl = this.getClass().getResource("");
    File file = new File(resourceUrl.toURI().toString() + "data.json");