{"id":864,"date":"2023-03-09T21:34:01","date_gmt":"2023-03-09T13:34:01","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=864"},"modified":"2023-04-29T16:45:08","modified_gmt":"2023-04-29T08:45:08","slug":"spring-boot-upload-file-java-io-ioexception-the-temporary-upload-location-error-resolution","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-upload-file-java-io-ioexception-the-temporary-upload-location-error-resolution\/","title":{"rendered":"Spring Boot\u4e0a\u4f20\u6587\u4ef6 java.io.IOException The temporary upload location \u9519\u8bef\u89e3\u51b3"},"content":{"rendered":"<p>\u4f7f\u7528Spring Boot\u6846\u67b6\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20\u65f6\u62a5\u9519\uff0c\u9519\u8bef\u5982\u4e0b\uff1a<\/p>\n<pre><code>Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [\/tmp\/tomcat.7366297186951596807.9000\/work\/Tomcat\/localhost\/ROOT] is not valid<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u9519\u8bef\u539f\u56e0<\/h2>\n<pre><code>\/tmp\/tomcat.7366297186951596807.9000\/work\/Tomcat\/localhost\/ROOT \u6587\u4ef6\u5939\u4e0d\u5b58\u5728<\/code><\/pre>\n<h2>\u9519\u8bef\u5206\u6790<\/h2>\n<p>\u5728Spring Boot\u9879\u76ee\u542f\u52a8\u540e\uff0c\u7cfb\u7edf\u4f1a\u5728<code>\/tmp<\/code>\u76ee\u5f55\u4e0b\u81ea\u52a8\u521b\u5efa\u51e0\u4e2a\u76ee\u5f55\uff1a<\/p>\n<ul>\n<li><code>hsperfdata_root<\/code><\/li>\n<li><code>tomcat.************.10002<\/code>(\u7ed3\u5c3e\u662f\u9879\u76ee\u7684\u7aef\u53e3)<\/li>\n<li><code>tomcat-docbase.*********.8080<\/code><\/li>\n<\/ul>\n<p>Multipart\uff08form-data\uff09\u7684\u65b9\u5f0f\u5904\u7406\u8bf7\u6c42\u65f6\uff0c\u9ed8\u8ba4\u5c31\u662f\u5728\u7b2c\u4e8c\u4e2a\u76ee\u5f55\u4e0b\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u7684<\/p>\n<p>\u8be6\u89c1\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/appblog.cn\/2020\/01\/23\/CentOS7\u7684tmp\u76ee\u5f55\u81ea\u52a8\u6e05\u7406\u89c4\u5219\/\" title=\"CentOS7\u7684\/tmp\u76ee\u5f55\u81ea\u52a8\u6e05\u7406\u89c4\u5219\">CentOS7\u7684\/tmp\u76ee\u5f55\u81ea\u52a8\u6e05\u7406\u89c4\u5219<\/a><\/p>\n<blockquote>\n<p>\u67e5\u9605\u8d44\u6599\u540e\uff0c\u53d1\u73b0\u662fCentOS\u5bf9<code>\/tmp<\/code>\u4e0b\u6587\u4ef6\u81ea\u52a8\u6e05\u7406\u7684\u539f\u56e0\u3002<code>\/tmp<\/code>\u6587\u4ef6\u5939\u6709\u4e2a\u7279\u6027\uff0c\u5c31\u662f\u572810\u5929\u5185\u6ca1\u6709\u8fdb\u884c\u64cd\u4f5c\uff0c\u8be5\u6587\u4ef6\u5939\u5185\u7684\u6587\u4ef6\u5939\u548c\u6587\u4ef6\u7b49\u5c31\u4f1a\u81ea\u52a8\u88ab\u6e05\u9664\uff0c\u5bfc\u81f4\u4e00\u4e9b\u6587\u4ef6\u4e22\u5931\u3002<\/p>\n<\/blockquote>\n<h2>\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<p>\u65b9\u6cd5\u4e00\uff1a\u76f4\u63a5\u91cd\u65b0\u542f\u52a8\u9879\u76ee\u5373\u53ef\u89e3\u51b3\uff08\u53ea\u80fd\u6cbb\u6807\u4e0d\u6cbb\u672c\uff09<\/p>\n<p>\u65b9\u6cd5\u4e8c\uff1a\u6307\u5b9a\u4e0a\u4f20\u6587\u4ef6\u4e34\u65f6\u7684\u8def\u5f84\uff08\u4ece\u6839\u672c\u89e3\u51b3\uff09\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>\u2460 \u5728\u542f\u52a8\u7c7b\u91cc\u9762\u6dfb\u52a0<code>MultipartConfigElement<\/code>\u914d\u7f6e\u7c7b<\/p>\n<pre><code class=\"language-java\">@Bean\nMultipartConfigElement multipartConfigElement() {\n    MultipartConfigFactory factory = new MultipartConfigFactory();\n    String location = System.getProperty(&quot;user.dir&quot;) + &quot;\/data\/tmp&quot;;\n    File tmpFile = new File(location);\n    if (!tmpFile.exists()) {\n        tmpFile.mkdirs();\n    }\n    factory.setLocation(location);\n    return factory.createMultipartConfig();\n}<\/code><\/pre>\n<p>\u2461 \u6216\u8005\u65b0\u5efa\u4e00\u4e2a\u914d\u7f6e\u7c7bMultipartConfig<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class MultipartConfig {\n\n    \/**\n     * \u6587\u4ef6\u4e0a\u4f20\u4e34\u65f6\u8def\u5f84\n     *\/\n    @Bean\n    MultipartConfigElement multipartConfigElement() {\n        MultipartConfigFactory factory = new MultipartConfigFactory();\n        String location = System.getProperty(&quot;user.dir&quot;) + &quot;\/data\/tmp&quot;;\n        File tmpFile = new File(location);\n        if (!tmpFile.exists()) {\n            tmpFile.mkdirs();\n        }\n        factory.setLocation(location);\n        return factory.createMultipartConfig();\n    }\n}<\/code><\/pre>\n<p>\u65b9\u6cd5\u4e09\uff1a\u5728propertites\u6587\u4ef6\u4e2d\u914d\u7f6e<\/p>\n<pre><code>spring.http.multipart.location=\u7f13\u5b58\u6587\u4ef6\u8def\u5f84<\/code><\/pre>\n<p>\u6216\u8005\u662fyml\u6587\u4ef6\u4e2d\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-yml\">spring:\n  http:\n    multipart:\n      max-file-size: 50Mb\n      max-request-size: 80Mb\n      location: F:\/appblog\/temp\n#     location: file:\/F:\/appblog\/temp  \/\/\u9519\u8bef\n      #location: ${user.home}\/upload<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Spring Boot\u6846\u67b6\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20\u65f6\u62a5\u9519\uff0c\u9519\u8bef\u5982\u4e0b\uff1a Could not parse multipar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}