无法在Java中创建缓存文件
问题描述:
我在我的网站上传图像时遇到问题。有时候,它会上传。但有时它也会说can’t create cache file
。我如何解决它?无法在Java中创建缓存文件
这里是我的代码:
File file = new File(imagePath);
BufferedImage bi;
try{
bi = ImageIO.read(file);
}catch(javax.imageio.IIOException e){
if(request.getParameter("fi") != null){
file = new File(context.getInitParameter("ImgPath") + "placeholder/150x80.png");
}else if (request.getParameter("li") != null){
file = new File(context.getInitParameter("ImgPath") + "placeholder/150x80.png");
}
bi = ImageIO.read(file);
}
OutputStream outImg = response.getOutputStream();
File cacheDir = new File(context.getInitParameter("ImgPath") + "cache");
try {
ImageIO.setCacheDirectory(cacheDir);
ImageIO.write(bi, "png", outImg);
} catch (Exception ex) {
}
outImg.close();
答
问题: Tomcat安装为未能创建在启动时或临时文件夹临时文件夹不可写。
解决方案:确保临时文件夹存在于\ temp下并且是可写的。您可以创建它手动。
或者
您可以覆盖默认临时文件夹通过在catalina.bat
(窗口)CATALINA_TMPDIR
环境变量或catalina.s
H(Linux)的设定值的Tomcat的位置。
# CATALINA_TMPDIR (Optional) Directory path location of temporary directory
# the JVM should use (java.io.tmpdir). Defaults to
# $CATALINA_BASE/temp.
Hi downvoter,你能发表评论吗? –