Uploading File Returns 403 Error - Spring MVC

前端 未结 4 1490
鱼传尺愫
鱼传尺愫 2021-01-05 02:25

In my Spring MVC project I am trying to upload a file via a simple form.

HTML Form:

4条回答
  •  失恋的感觉
    2021-01-05 02:58

    For me even with the csrf().disable() its didn't work. Once i disable it i got 200 but the file wasn't uploaded and i didn't see any error. Once i set the debug flag logging.level.org.springframework.web: DEBUG i saw the root cause:

    [org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/target/tomcat/work/Tomcat/localhost/ROOT] is not valid]","exception":""}

    I tried setting the location on application.yaml: spring.servlet.http.multipart.location: "/tmp" but it didn't change the location so what i did is adding the below code and it did the trick:

    @Bean

    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = "/tmp";
        LOGGER.debug("Multipart location file:" + location);
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
    

提交回复
热议问题