You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring图片上传报415 Unsupported Media Type错误求助

Troubleshooting "Unsupported Media Type (415)" Error in Spring Image Upload

Hey there, let’s tackle that frustrating 415 error together—file uploads in Spring can be finicky, but we’ll break down the most common fixes step by step.

Common Causes & Fixes

1. Frontend Request Format Mismatch

This is the #1 culprit for 415 errors in file uploads. When uploading a file, the browser needs to send the request with multipart/form-data encoding—without it, Spring can’t parse the file content.

  • Check your HTML form: Ensure your <form> tag includes enctype="multipart/form-data". This is mandatory for file uploads. Here’s a correct example:
    <form method="POST" action="/upload-image" enctype="multipart/form-data">
        <input type="file" name="uploadedImage" accept="image/*">
        <button type="submit">Upload Image</button>
    </form>
    
  • If using AJAX/Fetch: Don’t manually set the Content-Type header! Use a FormData object, and let the browser handle setting the correct multipart/form-data header with the necessary boundary. Manually setting the header without the boundary will break Spring’s parsing. Example:
    const fileInput = document.querySelector('input[type="file"]');
    const formData = new FormData();
    formData.append('uploadedImage', fileInput.files[0]);
    
    fetch('/upload-image', {
        method: 'POST',
        body: formData // No Content-Type header needed here!
    });
    

2. Missing or Misconfigured Multipart Support in Spring

Spring requires a MultipartResolver to handle file uploads. If it’s not set up correctly, the app can’t process multipart requests.

  • For Spring Boot: Make sure you have the spring-boot-starter-web dependency (it includes multipart support by default):
    <!-- pom.xml -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    You can tweak upload limits in application.properties if needed:
    spring.servlet.multipart.max-file-size=10MB
    spring.servlet.multipart.max-request-size=10MB
    
  • For non-Boot Spring: Explicitly define a CommonsMultipartResolver bean (you’ll also need the commons-fileupload dependency):
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(10485760); // 10MB
        return resolver;
    }
    

3. Incorrect Controller Method Setup

Your controller needs to accept the file the right way—wrong annotations or parameters will trigger a 415 error.

  • Use @RequestParam for the file: Don’t use @RequestBody here—we’re dealing with multipart form data, not JSON. Correct controller example:
    @PostMapping("/upload-image")
    public String handleImageUpload(@RequestParam("uploadedImage") MultipartFile imageFile) throws IOException {
        // Process the file: convert to bytes, save to DB, etc.
        byte[] imageBytes = imageFile.getBytes();
        // Add your database storage logic here
        return "upload-success";
    }
    
  • Avoid conflicting consumes attributes: If you added consumes = MediaType.APPLICATION_JSON_VALUE to your @PostMapping, remove it. Or explicitly set it to MediaType.MULTIPART_FORM_DATA_VALUE if needed.

4. Mismatched Parameter Names

Double-check that the name attribute in your frontend file input matches the value in @RequestParam. For example, if your input is <input name="profilePic">, your controller should use @RequestParam("profilePic") MultipartFile file.

5. Bonus: Database Storage Tips (Post-Fix)

Once you resolve the 415 error, make sure your database column is set to store binary data (like BLOB in MySQL, BYTEA in PostgreSQL). Example entity field:

@Lob
@Column(name = "image_data")
private byte[] imageData;

If you can share your actual backend controller code and frontend HTML/JS, we can pinpoint the exact issue even faster! But these steps cover 90% of the common 415 cases for Spring image uploads.

内容的提问来源于stack exchange,提问作者dorcsi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:17:50