SpringBoot內置Tomcat緩存文件目錄被意外刪除導致異常


在項目中,一般會將文件臨時保存到緩存目錄

當時使用

File.createTempFile("tmp", ext,
                        (File) request.getServletContext().getAttribute(ServletContext.TEMPDIR))

創建臨時文件時,項目一直運行正常,然而有一次報異常:

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: 
    The temporary upload location [/tmp/tomcat.7104877156386249310.8070/work/Tomcat/localhost/ROOT] is not valid

 

檢查文件目錄,文件確實不在,檢查代碼,也未發現問題。實在不知道原因,只有重啟了服務器,問題也就不再出現。

 

今天偶然查看官方文檔,發現問題所在,也提供了解決方法

    If you choose to use Tomcat on CentOS be aware that, by default, a temporary directory is
used to store compiled JSPs, file uploads etc. This directory may be deleted by tmpwatch
while your application is running leading to failures. To avoid this, you may want to customize 
your tmpwatch configuration so that tomcat.* directories are not deleted, or configure
server.tomcat.basedir so that embedded Tomcat uses a different location 

 

前往目錄 /etc/cron.daily/ 中,修改 tmpwatch 文件:

#! /bin/sh
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
        -X '/tmp/hsperfdata_*' 10d /tmp \
        -X '/tmp/tomcat.*' 10d /tmp
/usr/sbin/tmpwatch "$flags" 30d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 30d "$d"
    fi
done

 

添加一行

  -X '/tmp/tomcat.*' 10d /tmp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM