Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 770
Briana Avatar asked May 06 '17 13:05

Briana


2 Answers

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.sh (linux).

#   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
#                   the JVM should use (java.io.tmpdir).  Defaults to
#                   $CATALINA_BASE/temp.
like image 84
Sundararaj Govindasamy Avatar answered Oct 03 '22 07:10

Sundararaj Govindasamy


In which security context are your code in? In some implementation of 'multiple bundle on single virtual machine' framework, there can exist two types of bundles, one has permission to write temp folder, and the other does not. If the former would access ImageIO package first, ImageIO package determined that it would have permission to write temp folder, and trying to use it all of subsequent calls, but, in such frameworks, the latter can call ImageIO as well, and it will fail, since that bundle does not have access to the temp file. The behavior would change either the former would access first or vice versa, and if the latter would call ImageIO first, it won't ever use cache directory for that virtual machine instance and you don't see any troubles.

  • If your code does not have access to the cache directory and someone which has access to there calls ImageIO first, your code will fail.

And, it seems that your code would call ImageIO.setCacheDirectory() with your local data folder. In such frameworks, there are many cases that other bundles running on the same virtual machine would not have access to the local folder of your bundle. If so, they would have to throw IOException, if your setCacheDirectory() call was successful and you have given the directory only your code can access.

  • If your code successfully set the cache directory to your local data folder which other bundles cannot access to, your code might work fine, but other bundles would fail when trying to use ImageIO.
like image 27
MNEMO Avatar answered Oct 03 '22 08:10

MNEMO