2017-05-06 115 views
0

我在我的網站上傳圖像時遇到問題。有時候,它會上傳。但有時它也會說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(); 

回答

-1

問題: 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. 
+0

Hi downvoter,你能發表評論嗎? –