Spring StandardServletMultipartResolver

Spring StandardServletMultipartResolver

我想知道当您使用 StandardServletMultipartResolver 发送多部分请求时自动创建临时文件的代码在哪里?

我可以禁用该行为吗?我想决定它的存储方式和位置。我不想 spring 为我做这件事。

我正在考虑创建自己的解析器,但找不到有关如何禁用 spring 默认行为的信息。

引用自API docs StandardServletMultipartResolver 不支持解析器级别的临时文件配置,而是在 servlet 注册级别完成 -

In order to use Servlet 3.0 based multipart parsing, you need to mark the affected servlet with a "multipart-config" section in web.xml, or with a MultipartConfigElement in programmatic servlet registration, or (in case of a custom servlet class) possibly with a MultipartConfig annotation on your servlet class.

Configuration settings such as maximum sizes or storage locations need to be applied at that servlet registration level; Servlet 3.0 does not allow for them to be set at the MultipartResolver level.

因此,您可以在 servlet 上配置它,或者切换到 CommonsMultipartResolver,它支持开箱即用地设置临时目录,因为它继承自 CommonsFileUploadSupport.setUploadTempDir (查看各自的文档 here and here)