0

In build a web application with netbeans, in some point I extract an arrayList to json format.

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();
        }
}

and this creates the file C:\temp\data.json. How can I create this file inside my project folder where JSPS's exist? I tried determine the location with

String dir = System.getProperty("user.dir");

but this created the file in C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.15\bin. I want to create the file inside the projects folder in order to use it to show my data in bootstrap table. There I define the location of my data by

data-url="data.json"

I tried this data-url="C:\temp\data.json" but i got an error

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.
yaylitzis
  • 5,354
  • 17
  • 62
  • 107
  • Your 'project folder' exists on your development box. When you deploy your application it won't be there. What you need is a temporary directory, or a directory usable by the deployed webapp. You're asking the wrong question altogether. – user207421 Nov 11 '15 at 22:31

2 Answers2

1

This is cross domain problem, bootstrap table does not support file:// url, please use an web server to run your code. Here is a issue can help you: https://github.com/wenzhixin/bootstrap-table/issues/230

wenyi
  • 1,384
  • 8
  • 11
0

To get a File inside the project's folder you can use

    URL resourceUrl = this.getClass().getResource("");
    File file = new File(resourceUrl.toURI().toString() + "data.json"); 
Nenad
  • 484
  • 3
  • 14