首页 > 其他分享 >文件上传之本来好好的上传为什么忽然无法上传了

文件上传之本来好好的上传为什么忽然无法上传了

时间:2023-03-12 09:57:02浏览次数:30  
标签:tmp 文件 systemd 好好 private 上传 目录 tmpdir

前置条件

springboot单jar包后端服务,有个功能,需要导入文件。测试通过了,也发到了现场,感觉很好。忽然有一天,现场丢过来一个报错

排查过程

先在公司服务器上了试了下,复现了。本地起了一个,没复现。哈哈,典型的我本地没问题啊。

然后搜了下这个路径,发现其实大多数中间件,不管是tomcat、jetty、tas、还是tongweb,都会创建一个临时目录。父路径为System.getProperty("java.io.tmpdir"),在linux中对应/tmp目录(也可以通过jinfo pid| grep "java.io.tmpdir"查看)。

启动的时候会调用AbstractConfigurableWebServerFactory#createTempDir来创建一个/tmp/tongweb.3897351644260594220.8887这样的目录。

protected final File createTempDir(String prefix) {
		try {
			File tempDir = File.createTempFile(prefix + ".", "." + getPort());
			tempDir.delete();
			tempDir.mkdir();
			tempDir.deleteOnExit();
			return tempDir;
		}
		catch (IOException ex) {
			throw new WebServerException(
					"Unable to create tempDir. java.io.tmpdir is set to " + System.getProperty("java.io.tmpdir"), ex);
		}
	}

你上传的文件也会临时存储在这个文件夹下。

题外话,还会创建一个tongweb-docbase.4901402598652682849.8887这样的文件夹

但是,linux 的tmp目录10天之后会清除。/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 看这行
q /tmp 1777 root root 10d
q /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

# Remove top-level private temporary directories on each boot
R! /tmp/systemd-private-*
R! /var/tmp/systemd-private-*

也就是,如果长时间没用,这个目录被清空了,但是上传文件还是往这个目录上传,就会找不到这个目录了,所以就找不到。

解决方案

  1. 创建这个临时文件夹,临时解决。
  2. 环境参数设置-Djava.io.tmpdir=/opt/tmp建立如/opt/tmp这个目录
  3. application.yml文件中配置如下,不过要确保这个路径存在
spring:
  servlet:
    multipart:
      location: /upload/file

这个配置只是更改了上传文件临时目录,原来启动创建的目录仍旧是java.io.tmpdir对应的目录。而且,针对windows开发情况,路径就对应c://upload/file需要手动创建文件夹。

  1. 通过注入MultipartConfigElementbean的方式。
@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    String path = System.getProperty("user.dir");
    File tmpFile = new File(path);    //需要修改:选择要创建的路径,不然直接放user.dir下了
    if (!tmpFile.exists()) {
        tmpFile.mkdirs();
    }
    // 明确指定上传文件的临时目录
    factory.setLocation(path);
    return factory.createMultipartConfig();
}

总体来说,1最简单但只能作为临时方案。推荐方案2和方案4.方案3针对服务器和本地开发两套环境有些不方便。

标签:tmp,文件,systemd,好好,private,上传,目录,tmpdir
From: https://www.cnblogs.com/studentytj/p/17207644.html

相关文章

  • Docker json文件配置
    点击查看代码[root@master0~]#cat/etc/docker/daemon.json{"exec-opts":["native.cgroupdriver=systemd"],"log-driver":"json-file","log-opts":{......
  • 大文件断点续传如何实现?
    各位看官大家好,今天给大家分享的又是一篇实战文章,希望大家能够喜欢。开味菜最近接到一个新的需求,需要上传2G左右的视频文件,用测试环境的OSS试了一下,上传需要十几分钟,再......
  • 二、分布式文件系统概述
    ​ 1分类1.1通用分布式文件系统​和传统的本地文件系统(如ext3、NTFS等)相对应。典型代表:lustre、MooseFS1.1.1优点​标准文件系统操作方式,对开发者门槛较低1.1.......
  • 通过MapEdit源程序的学习MAP文件 1,需要安装的控件DXD7,WIL
       文件打开后是这个样子的,开大地图很慢,放大缩小大地图也很慢。我怀疑没有用到DXD7,只是因为WIL部件需要? 打开源程序需要WIL部件。于是安装,找的资料,上次安......
  • 欧陆战争6:1804 战役文件参考
    本文为战役btl文件内容参考,曾发布于百度贴吧。说明:1.一般的,本文数字为16进制数字,除非另有说明。2.本文涉及的ID编号可在json文件中找到,默认为10进制数字,在btl中填写前......
  • 通过HH8WilEdit学习WIL 文件编码 12 对WIL文件调色板的读取
    unitWilPalette;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,ExtCtrls,StdCtrls;typeTRGBQua......
  • 使用virt-manager 在主机和客户机之间共享文件夹
    导读在本指南中,你将学习如何在virt-manager的KVM、QEMU和libvirt的主机和客户机之间共享文件夹。​​virt-manager​​​应用或软件包使用​​libvirt​......
  • 怎么写.md文件
    一级标题#+文字二级标题##文字文字 Hello,world!两边加星号*加粗Hello,world!两边加一个星号斜体Hello,world!两边加三个星号斜体加粗Hello,world!两......
  • Zabbix监控Tomcat开启JMX时修改catalina.sh文件后tomcat服务起不来/配置catalian.sh不
    zabbix监控tomcat,开启JMX修改catalina.sh文件,tomcat服务起不来问题现象(配置Catalina.sh脚本,开启JMX不起效)问题一:配置完脚本,重启tomcattomcat服务启动失败查看tomcat的ca......
  • git将本地文件传到gitee中
    一,文件上传到gitee1.先在gitee中新建一个仓库获取地址:[email protected]:weixinwen/practice.git如下图2.简易的命令行入门教程:Git全局设置:gitconfig--globaluser.......