luka93geo
By:
luka93geo

How to save a file with java on tomcat (centOS)

November 11, 2015 1.3k views
Java Deployment

Hello, i apologize myself in advance if i do not provide enough information.
I have setup tomcat on centOS and managed to put my java web application there. The point of that app is to fill out a form where the data will be put in a .json file on the server on upload button. The thing is this , somehow i can not make it work on the server but it works in a local tomcat server. Could you help me?
Here is my servlet code for updating the .json file :

try {
String desc = request.getParameter ("description");
String title = request.getParameter("title");
String cat = request.getParameter ("categorie");
String[] subCat = request.getParameterValues ("subCategory");
String jsonPath = getServletContext().getRealPath("\");
JSONObject obj = new JSONObject ();

        obj.put("title",title);
        obj.put("description", desc);
        obj.put("category", cat);

        JSONArray list = new JSONArray();
        for (int i = 0;i<subCat.length;i++){
            list.add(subCat[i]);
        }
        obj.put("subCategory",list);
         try {
    FileWriter file = new FileWriter("/root/opt/tomcat/webapps/ROOT/test.json",true); -----> i have tried this with jsonPath+ "test.json" aswell
            BufferedWriter bufferWritter = new BufferedWriter(file);
            bufferWritter.write(obj.toJSONString());
            bufferWritter.flush();
            bufferWritter.close();
           response.sendRedirect("index.jsp");

} catch (IOException e) {
    e.printStackTrace();
            out.print("ERROR");
}
    } finally {
        out.close();
    }
Be the first one to answer this question.