1

In my Servlet I need the client to upload a video or image in a form. I succeeded in uploading the file to the server in the following code. appPath is what I used first, however this is a bad path to permanently store these files since they will be deleted with every redeploy. For now I store them in the bin folder (appPath2). What folder would you guys suggest uploading to?

    String appPath = request.getServletContext().getRealPath("");
    String appPath2 = "path to tomcat folder/apache-tomcat-9.0.1/bin";
    // constructs path of the directory to save uploaded file
    String savePath = appPath2 + File.separator + "NotificationVideos";
    File fileSaveDir = new File(savePath);
    if (!fileSaveDir.exists()) {
        fileSaveDir.mkdir();
    }

    String fileName = extractFileName(request.getPart("file"));
    fileName = new File(fileName).getName();
    this.createVideo((savePath + File.separator + fileName), notificationService.getHighestId());
    request.getPart("file").write(savePath + File.separator + fileName);

}

private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            return s.substring(s.indexOf("=") + 2, s.length()-1);
        }
    }
    return "";
}`

Now my Actual Question:

I need to acces these videos or images in my JSP page. I save the path, "savePath + fileName" in a database. And I give the fileName as an attribute to the JSP page when loading the JSP page.

<video preload="auto" controls>
   <source src="/NotificationVideos/${imageSrc}" type="video/mp4">
</video

It works great when i use a folder in the webapps folder. However i can't seem to make it work from a permanent folder outside webapps. I also tried

<Context docBase="path to tomcat folder/apache-tomcat-9.0.1/bin/NotificationVideos" path="/NotificationVideos"/>

Any ideas on how I can fix this?

0 Answers0