简体   繁体   English

Java Spring boot 2.0.5 MultipartFile上传“内容类型不受支持”

[英]Java Spring boot 2.0.5 MultipartFile upload “Content type not supported”

I have a project with Spring boot and I'm trying to upload a file. 我有一个使用Spring Boot的项目,我正在尝试上传文件。

I'm using multipart/form-data on my form and @RequestPart in my Controller. 我在表单上使用multipart / form-data,在Controller中使用@RequestPart。 Although all the tutorials and solutions recommended to use @RequestParam, it doesn't seem to work anymore or just for me. 尽管所有教程和解决方案都建议使用@RequestParam,但它似乎不再起作用或仅对我有用。

I got over the problem of not getting the post request parameter by using @RequestPart but now the browser gives @Content type not supported" error. 我克服了无法通过使用@RequestPart获取发布请求参数的问题,但是现在浏览器给出了@Content type not support”错误。

HTML 的HTML

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/4.1.3/css/bootstrap.min.css"/>
    <script type="text/javascript" src="/webjars/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    <title>Title</title>
</head>
<body>
<div class="container">
    <div class="card">
        <div class="card-body">
            <div class="offset-3 col-6">
                <form method="post" action="/save" enctype="multipart/form-data">
                    <input type="file" class="form-control" name="file">
                    <input type="submit" class="btn btn-primary" value="Upload">
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Controller 控制者

package com.tantsurepertuaar.tantsurepertuaar.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;




@Controller
public class TestController {

    @PostMapping("/save")
    public String save(@RequestPart("file") MultipartFile file) {
        //saving file
        return "redirect:/";
    }
}

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tantsurepertuaar</groupId>
    <artifactId>tantsurepertuaar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>tantsurepertuaar</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-security</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.security</groupId>-->
            <!--<artifactId>spring-security-test</artifactId>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.1.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I have a feeling that it might have something to do with being newer versions of Spring boot for example. 我有一种感觉,例如它可能与Spring Boot的较新版本有关。 I tried out example on page http://www.mkyong.com/spring-boot/spring-boot-file-upload-example/ and it worked but when I used the same HTML and Controller in my project then I got the same error as before. 我在页面http://www.mkyong.com/spring-boot/spring-boot-file-upload-example/上尝试了示例,它可以正常工作,但是当我在项目中使用相同的HTML和Controller时,我得到了相同的结果与以前一样的错误。

Thanks 谢谢

and add the following in application.properties 并在application.properties中添加以下内容

multipart.enabled=true
multipart.file-size-threshold=0 # Threshold after which files will be written to disk.
multipart.location=# Intermediate location of uploaded files.
multipart.max-file-size=1Mb # Max file size.
multipart.max-request-size=10Mb # Max request size.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot 中的“bodyType=org.springframework.web.multipart.MultipartFile 不支持内容类型‘image/jpeg’” - "Content type 'image/jpeg' not supported for bodyType=org.springframework.web.multipart.MultipartFile" in Spring Boot 使用Spring Boot的Spring Rest:将MultipartFile和Json对象作为参数上传 - Spring rest with Spring Boot: Upload MultipartFile and Json object as parameters Spring Boot POST PDF内容类型&#39;application / pdf不支持 - Spring boot POST PDF Content type 'application/pdf not supported 终端中的POST请求不支持Spring Boot内容类型&#39;text / plain&#39; - Spring Boot Content type 'text/plain' not supported in POST request in terminal Spring 启动 MultipartFile 方法 - Spring boot MultipartFile methods Spring Boot MultipartFile 上传 getOriginalFileName 因浏览器而异 - Spring Boot MultipartFile upload getOriginalFileName different depending on browser Spring 3.0 MultipartFile上传 - Spring 3.0 MultipartFile upload 从Spring Boot 2.0.5 / Java 8迁移后,Spring Boot 2.1 / Java 11应用程序无法识别突出字符 - Spring boot 2.1 / java 11 app not recognizing accentuated characters after migrating from Spring boot 2.0.5 / java 8 java - 如何使用spring boot在java中将multipartfile参数设置为“不需要”? - How to set a multipartfile parameter as "not required" in java with spring boot? Spring Boot 中 MultipartFile 的最大限制 - Max limit of MultipartFile in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM