【问题标题】:Tomcat WebApp: Error storing image file outside of context root on OpenShiftTomcat WebApp:在 OpenShift 上将图像文件存储在上下文根之外时出错
【发布时间】:2013-05-24 16:19:36
【问题描述】:

为了安全起见,我的 tomcat Web 应用程序上传图像并将它们保存在上下文文件夹之外。我在本地机器上测试了代码,它运行良好。

当我在 OpenShift 上托管代码时,在下面突出显示的行上出现空指针异常:

public void init() throws ServletException {
    // get name of images directory
    String imagesPath = getServletContext().getInitParameter(
            PARAM_UPLOAD_IMAGE_PATH);

    //get path to context root directory
    String contextRoot = getServletContext().getRealPath("/");



    //remove trailing '/'
    contextRoot = contextRoot.substring(0,contextRoot.length() - 1);//NULL POINTER EXCEPTON

    //get path of directory outside root, where images will be saved.
    String outsideRoot = contextRoot.substring(0, contextRoot.lastIndexOf("/"));

    uploadPath = outsideRoot + File.separator + imagesPath;
}

我不明白问题出在哪里,因为代码在我的机器上运行良好。托管站点是否不允许您将文件保存在上下文根目录之外?有没有解决的办法?感谢您的帮助!

【问题讨论】:

    标签: tomcat web-deployment openshift


    【解决方案1】:

    您在 OpenShift 上实际拥有写入权限的唯一位置是 ~/app-root/data 和 /tmp。

    App-root/data 有一个环境变量,您应该在代码 $OPENSHIFT_DATA_DIR 中使用。请更改您的代码以写入此目录。

    【讨论】:

    • 谢谢,我可以直接说File file = new File("$OPENSHIFT_DATA_DIR/MyApp/image.jpg") 就可以了?
    猜你喜欢
    • 1970-01-01
    • 2011-10-03
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2011-02-02
    • 2011-09-18
    相关资源
    最近更新 更多