I need to save a file to a server using Java
. In my case, I need to save a json
file. Can I save a file normally on my computer, but how do I save directly to a remote server? Here is an example that saves a file to my computer. My question is: to save this file to a remote server, how could I do it?
public class EstudoJSON {
public static void main(String[] args) throws IOException {
JSONObject json = adicaoSimplesDeDados ();
FileWriter file = new FileWriter("json//javaJSON.json");
file.write(json.toString());
file.flush();
file.close();
System.out.println(json.toString());
adicaoDeUmObjeto ();
}
public static JSONObject adicaoSimplesDeDados () {
Carro carro = new Carro();
carro.setId(1l);
carro.setModelo("Celta");
carro.setPlaca("AAA1234");
//Criacao do objeto carroJson
JSONObject carroJson = new JSONObject();
//Insercao de valores do carro no objeto JSON
carroJson.put("id", carro.getId());
carroJson.put("Modelo", carro.getModelo());
carroJson.put("Placa", carro.getPlaca());
System.out.println(carroJson);
System.out.println(carroJson.get("Modelo"));
return carroJson;
}
public static void adicaoDeUmObjeto () {
Carro carro = new Carro();
carro.setId(1l);
carro.setModelo("Ka");
carro.setPlaca("AAA1234");
JSONObject carroJson = new JSONObject();
carroJson.put("Carro", carro);
System.out.println(carroJson);
System.out.println(carroJson.get("Carro"));
}
}