Bharat Nanwani
Bharat Nanwani

Reputation: 703

Permission Denied on Tomcat

Can you please help me with below. I have a 'DownloadFile' Servlet which lets you download a CSV file. The Servlet is working on my local Windows Machine however, on my server, it is throwing an error, permission to denied to the Download File path.

Tomcat is installed on Tomcat User. Tomcat user is the owner of few folders, however, I'm still getting permission denied issue.

Below is the code and error:

File f = new File("\\opt\\tomcat\\logs\\myfile.csv");
         int length = 0;
          ServletOutputStream op = response.getOutputStream();
          ServletContext context = getServletConfig().getServletContext();
          String mimetype = context.getMimeType("text");

        response.setContentType((mimetype != null) ? mimetype: "application/octet-stream");
        response.setContentLength((int) f.length());
        response.setHeader("Content-Disposition","attachment; filename=csv1.csv");

Error:

ype Exception report

message \opt\tomcat\logs\myfile.csv (Permission denied)

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.io.FileNotFoundException: \opt\tomcat\logs\myfile.csv (Permission denied)
    java.io.FileOutputStream.open0(Native Method)
    java.io.FileOutputStream.open(FileOutputStream.java:270)
    java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    java.io.FileWriter.<init>(FileWriter.java:63)
    in.travelfiles.Csv1.doGet(Csv1.java:51)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Upvotes: 0

Views: 1433

Answers (1)

stdunbar
stdunbar

Reputation: 17435

On a Unix based environment the file separator is the forward slash. A Unix machine isn't going to like the backslash character. When you build a file path string, Java allows you to create this in an O/S independent way using something like:

String fileSeparator = System.getProperty("file.separator");
String fileName = fileSeparator + "opt" + fileSeparator + "tomcat" +
                  fileSeparator + "logs" + fileSeparator + "myfile.csv";

The fileName will work on either Windows or Unix.

I'd start with that to see if you can read the file. It's still possible that the O/S won't let the user running Tomcat to read the file but first get it into a path that Unix can understand.

Upvotes: 2

Related Questions