我在网站上上传图片时遇到问题。有时,它会上传。但有时它也会说无法创建缓存文件
。我该如何修复它?
这是我的代码:
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_TMPDIR
的值来覆盖默认 Tomcat 临时文件夹位置。 catalina.bat
中的环境变量(Windows)或catalina.s
h(Linux)。
# CATALINA_TMPDIR (Optional) Directory path location of temporary directory
# the JVM should use (java.io.tmpdir). Defaults to
# $CATALINA_BASE/temp.
关于java - 无法在Java中创建缓存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820997/