springboot /tmp 臨時(shí)目錄的具體實(shí)現(xiàn)
1.生成機(jī)制
在linux系統(tǒng)中,springboot應(yīng)用服務(wù)再啟動(dòng)(java -jar 命令啟動(dòng)服務(wù))的時(shí)候,會(huì)在操作系統(tǒng)的/tmp目錄下生成一個(gè)tomcat*的文件目錄,上傳的文件先要轉(zhuǎn)換成臨時(shí)文件保存在這個(gè)文件夾下面。
因?yàn)榱魅∫淮蜗M(fèi)之后,后面無(wú)法再?gòu)牧髦蝎@取數(shù)據(jù),所以緩存方便后續(xù)復(fù)用;
2.產(chǎn)生異常
上線后可能tomcat臨時(shí)文件夾會(huì)被Linux刪除,會(huì)報(bào)找不到錯(cuò)誤,現(xiàn)在趕緊記錄一下,已被不時(shí)之需
cat /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 v /var/tmp 1777 root root 30d # 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
3.解決辦法
3.1 重啟大法
既然目錄被刪除了,重啟一下服務(wù),讓系統(tǒng)重新生成該目錄,臨時(shí)解決(但是以后目錄還可能被刪除)
3.1 從Linux層面修改 /tmp目錄的清理策略
配置一下不刪除tmp目錄下的tomcat
vim /usr/lib/tmpfiles.d/tmp.conf # 添加下面一行 x /tmp/tomcat.* # 重啟服務(wù) systemctl restart systemd-tmpfiles-clean
3.2 增加JVM配置
#定臨時(shí)目錄為/app/xxx/tmp -Djava.io.tmpdir=/app/xxx/tmp(自定義路徑)
3.3 增加JVM配置
-java.tmp.dir=/data/upload_tmp
3.4 添加spring boot配置
spring: http: multipart: location: /data/upload_tmp
3.5 使用配置類配置
在Spring容器中注冊(cè)MultipartConfigElement對(duì)象,通過(guò)MultipartConfigFactory指定路徑,路徑不存在的話就創(chuàng)建
@Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); String location = System.getProperty("user.dir")+"/data/tmp"; File tmpFile = new File(location); if (!tmpFile.exists()){ tmpFile.mkdirs(); } factory.setLocation(location); return factory.createMultipartConfig(); }
到此這篇關(guān)于springboot /tmp 臨時(shí)目錄的具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot /tmp 臨時(shí)目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
聊聊Spring?Boot如何配置多個(gè)Kafka數(shù)據(jù)源
這篇文章主要介紹了Spring?Boot配置多個(gè)Kafka數(shù)據(jù)源的相關(guān)知識(shí),包括生產(chǎn)者、消費(fèi)者配置,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10Java多線程模擬銀行系統(tǒng)存錢問(wèn)題詳解
本文將利用Java多線程模擬一個(gè)簡(jiǎn)單的銀行系統(tǒng),使用兩個(gè)不同的線程向同一個(gè)賬戶存錢。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09Java 17 更新后的 strictfp 關(guān)鍵字
strictfp 可能是最沒(méi)有存在感的關(guān)鍵字了,很多人寫了多年 Java 甚至都不知道它的存在,strictfp,字面意思就是嚴(yán)格的浮點(diǎn)型。這玩意兒居然還有個(gè)關(guān)鍵字,可見(jiàn)其地位還是很高的。下面文章小編就帶大家詳細(xì)介紹其關(guān)鍵字,需要的朋友可以參考一下2021-09-09java防盜鏈在報(bào)表中的應(yīng)用實(shí)例(推薦)
下面小編就為大家?guī)?lái)一篇java防盜鏈在報(bào)表中的應(yīng)用實(shí)例(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09