简体   繁体   中英

Can’t create cache file in Java

I'm having trouble uploading an image in my website. Sometimes, it uploads. But sometimes it also says can't create cache file . How do I fix it?

Here's my code:

    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();

Problem: Your tomcat installation is failed to create temp folder on startup or temp folder is not writable.

Solution: Make sure temp folder exists under \\temp and it is writable.

You can create it manually .

Or

You can override default temp folder location of Tomcat by setting the value for CATALINA_TMPDIR environment variable in catalina.bat (windows) or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM