在tomcat webapp文件夹之外的jsp中的图像

时间:2018-02-15 20:40:08

标签: java jsp tomcat servlets web-applications

在我的Servlet中,我需要客户端在表单中上传视频或图像。我成功地通过以下代码将文件上传到服务器。 appPath是我首先使用的,但是这是永久存储这些文件的错误路径,因为它们将在每次重新部署时被删除。现在我将它们存储在bin文件夹(appPath2)中。你们建议上传到哪个文件夹?

    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 "";
}`

现在我的实际问题:

我需要在JSP页面中访问这些视频或图像。我保存了路径," savePath + fileName"在数据库中。在加载JSP页面时,我将fileName作为属性提供给JSP页面。

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

当我在webapps文件夹中使用文件夹时,它非常有用。但是我似乎无法使用webapps之外的永久文件夹。我也试过

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

关于如何解决此问题的任何想法?

0 个答案:

没有答案