获取我的项目文件夹的路径

Get the path of my project's folder

在使用 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 中创建了文件。我想在项目文件夹中创建文件,以便使用它在 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.

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

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

这是跨域问题,bootstraptable不支持file://url,请使用网络服务器运行您的代码。这里有一个问题可以帮助你:https://github.com/wenzhixin/bootstrap-table/issues/230