为了安全起见,我的 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;
}
我不明白问题出在哪里,因为代码在我的机器上运行良好。托管站点是否不允许您将文件保存在上下文根之外?有没有解决的办法?感谢您的帮助!
最佳答案
您在 OpenShift 上实际拥有写入权限的唯一位置是 ~/app-root/data 和/tmp。
App-root/data 有一个环境变量,你应该在你的代码中使用 $OPENSHIFT_DATA_DIR。请更改您的代码以写入此目录。
关于Tomcat 网络应用程序 : Error storing image file outside of context root on OpenShift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16739471/