0
votes

I am using an API which creates a file in the tomcat bin folder, I have created a servelet to make the file downloadable but what path should I give to point to the bin folder? The website URL is like

abc.co.uk/test/home.jsp

Here is my servlet code:

private static final int DEFAULT_BUFFER_SIZE = 102400; //100KB
private String filePath;

public void init() throws ServletException
{
    this.filePath = getServletContext().getRealPath("/");
    System.out.println("Original Path: " + filePath + ": " + this.filePath.indexOf("webapps") + 1);
    this.filePath = this.filePath.substring(0, this.filePath.indexOf("webapps")) + "home/";
    System.out.println("FilePath1: " + this.filePath);
    //"C:\\Users\\Admin\\Documents\\NetBeansProjects\\MSc\\model";
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String requestedFile = request.getPathInfo();

    // Check if file is actually supplied to the request URI.
    if (requestedFile == null) {
        // Do your thing if the file is not supplied to the request URI.
        // Throw an exception, or send 404, or show default/warning page, or just ignore it.
        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
        return;
    }

    // Decode the file name (might contain spaces and on) and prepare file object.
    File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8"));

    // Check if file actually exists in filesystem.
    if (!file.exists()) {
        // Do your thing if the file appears to be non-existing.
        // Throw an exception, or send 404, or show default/warning page, or just ignore it.
        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
        return;
    }

    // Get content type by filename.
    String contentType = getServletContext().getMimeType(file.getName());

    // If content type is unknown, then set the default value.
    // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
    // To add new content types, add new mime-mapping entry in web.xml.
    if (contentType == null) {
        contentType = "application/octet-stream";
    }

    // Init servlet response.
    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType(contentType);
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

    // Prepare streams.
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        // Open streams.
        input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally {
        // Gently close streams.
        close(output);
        close(input);
    }



    processRequest(request, response);
} 

And here is the anchor tag code:

  <a href="./final.cmp">DOWNLOAD MODEL FILE!!!</a>

I need some assistance.

1
This is not good idea to create files like this in bin folder. You should use a separate dedicated folder for that. For that folder, you can write a special request handler to serve file content.spgodara
You should not be touching anything in the /bin folder. Rethink this.duffymo
As a side-note: when I created a webservice app that runs Unit tests, the default location that it puts test reports is in the /bin folder. So, its probably not too unusual of a request. Of course I set the test runner to put them elsewhere but I can easily see how a person would encounter this problem.djangofan

1 Answers

1
votes

The tomcat bin directory can be accessed via the environment variable CATALINA_BASE or the system property catalina.base

eg:

System.getProperty("catalina.base") + File.separator + "bin"

Generating files in the bin directory sounds like one of the worst places to put them though. You'd be best off putting generated files in a directory of their own where you can separate out the permission requirements.