springboot项目上传文件出现临时文件目录为空

【自取】最近整理的,有需要可以领取学习:

最近,前端的FormData上传了编写要上传到服务器上进行读取的文件的代码,服务器端使用MultipartFile进行接收。在测试了MultipartFile对象中的内容之后,可以找到常规属性。测试getInputStrea()方法时出现以下错误。简单查看一下这是什么目录,但您以前从未见过它:[en]Recently, the code for writing a file to be uploaded to the server to be read is uploaded by FormData at the front end and received by the server with MultipartFile. After testing what is in the MultipartFile object, the general attributes can be found. The following error occurred when testing the getInputStrea () method. Take a simple look at what directory this is, but you have never seen it before:

springboot项目上传文件出现临时文件目录为空

过了一段时间,百度发现这是存放临时文件的路径,并不清楚该放在这个目录下是什么,但这个目录在本机上是不可用的。在Linux系统下,似乎每10天就会清空系统一次。我在API下查看了这一点。[en]After some time, Baidu found that this is the path where temporary files are stored, and it is not clear according to what to put it in this directory, but this directory is not available on this machine. Under the linux system, it seems that the system will be emptied once every 10 days. I looked through this under API.

springboot项目上传文件出现临时文件目录为空

百度还发现了另一个由Spring配置的坑。[en]Baidu also found another pit, configured by spring.

@Beanpublic MultipartConfigElement multipartConfigElement(){   MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();   String location = System.getProperty("user.dir") + "/data/tmp";   File file = new File(location);   if(!file.exists()){      file.mkdirs();   }   multipartConfigFactory.setLocation(location);   return multipartConfigFactory.createMultipartConfig();}
然后调试,我发现我在getInputStream()中设置了路径:[en]<u>Then debug, I found that I set the path in getInputStream ():</u>

springboot项目上传文件出现临时文件目录为空

如果设置了大小,但未设置路径,则可以将MultipartFile设置为文件来执行该操作。[en]If the size is set, but the path is not set, you can set MultipartFile to File to do the operation.

最后,如果代码不知道如何读取API,它会突然变得清晰起来。[en]<u>Finally, if the code doesn't know how to read API, it suddenly becomes clear.</u>

undefined

Original: https://www.cnblogs.com/jpfss/p/12193245.html
Author: 星朝
Title: springboot项目上传文件出现临时文件目录为空

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6133/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部