How to write file to ubuntu linux server ? Java 11

Solution for How to write file to ubuntu linux server ? Java 11
is Given Below:

I did deploy java web application writing with spring to ubuntu linux server via Tomcat apache.
I created folder to store all the downloads in the tomcat path inside the linux server folders :
Path : ” opt/tomcat/apache-tomcat-9.0.46/work/downloads/ “

When I try to download file from external API to the ubuntu directory ( Above path )
I get Error 500 external, caused by the server not finding the path.

Method code: ( Java )

@RequestMapping(value = "/downloadDataDump", method = RequestMethod.GET)
public String downloadDataDump(@RequestParam(value = "fileName") String fileName) throws IOException {

    String fileUrl = null;
    URL url = null;
    HttpURLConnection con = null;
    try {
        fileUrl = getHotelDataDumpUrl("all", "en").getBody().getData().getURL();
        url = new URL(fileUrl);
        con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("Accept-Encoding", "zstd");
    } catch (IOException e) {
        } catch (ParseException e) {
    FileOutputStream fout = null;
    ZstdInputStream reader = null;
    try {
        reader = new ZstdInputStream(con.getInputStream());
        fout = new FileOutputStream("//" + fileName);
    } catch (FileNotFoundException e) {

Stack trace (from comment): /opt/tomcat/apache-tomcat-9.0.46/work/downloads/hotelNew (Permission denied)
   at java.base/ Method)
   at java.base/
   at java.base/<init>(
   at java.base/<init>(

You have a permission problem:

  • find out which user is Tomcat running as: it’s the user of the java process,
  • open a shell as Tomcat’s user (let’s say it is tomcat):
    sudo -u tomcat /bin/bash
  • try creating a file:
    touch /opt/tomcat/apache-tomcat-9.0.46/work/downloads/hotelNew

To create a file Tomcat needs traversal (x) permission to all directories and write (w) permission on the downloads folder.