Spring Boot ERROR-The temporary upload location is not valid

10/27/2021 spring bootlinuxdevOps

# Spring Boot ERROR-The temporary upload location is not valid

# 问题描述

线上系统报了个错误


Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;nested exception is org.springframework.web.multipart.MultipartException: 
Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.8327527609713611085.10001/work/Tomcat/localhost/ROOT] is not valid] with root cause

java.io.IOException: The temporary upload location [/tmp/tomcat.8327527609713611085.10001/work/Tomcat/localhost/ROOT] is not valid
1
2
3
4
5

# 处理办法

通过排查分析发现线上系统主机是centos7.5,而且centos有定时清理/tmp目前的行为特此记录下centos清除/tmp的规则 CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统服务有3个:

systemd-tmpfiles-setup.service  :Create Volatile Files and Directories
systemd-tmpfiles-setup-dev.service:Create static device nodes in /dev
systemd-tmpfiles-clean.service :Cleanup of Temporary Directories
1
2
3

相关配置文件也有3个地方

/etc/tmpfiles.d/*.conf
/run/tmpfiles.d/*.conf
/usr/lib/tmpfiles.d/*.conf
1
2
3

/tmp目录的清理规则主要取决于/usr/lib/tmpfiles.d/tmp.conf文件的设定,默认的配置内容为:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d           #   清理/tmp下10天前的目录和文件
v /var/tmp 1777 root root 30d       #   清理/var/tmp下30天前的目录和文件

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

我们可以配置这个文件,由于我们使用的是Spring Boot,则可以配置让系统不自动清理/tmp下以tomcat开头的目录,那么增加下面这条内容到配置文件中即可:

x /tmp/tomcat.*
1

更多详细配置请参考 tmpfiles.d 中文手册 (opens new window)

Last Updated: 10/27/2021, 10:29:45 AM