最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - 413 request entity too large Spring Boot - Stack Overflow

programmeradmin0浏览0评论

I have been creating an endpoint to which one could upload multipart files till 50MB but is throwing the 413 request entity too large error,,

It worked for small files of 500kb but sent this error for 28MB file, but I have set the max-size to be 50MB

application.properties:

spring.application.name=pgm

# multipart config

# enable multipart uploads
spring.servlet.multipart.enabled=true
# max file-size
spring.servlet.multipart.max-file-size=50MB

logging.level.springframework.web=DEBUG
server.tomcat.max-swallow-size=50MB


The main app file:

package com.mono.pgm;

import .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PgmApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(PgmApplication.class, args);
    }
}

The controller:

package com.mono.pgm;

import .springframework.beans.factory.annotation.Autowired;
import .springframework.http.HttpStatus;
import .springframework.http.ResponseEntity;
import .springframework.web.bind.annotation.GetMapping;
import .springframework.web.bind.annotation.PostMapping;
import .springframework.web.bind.annotation.RequestParam;
import .springframework.web.bind.annotation.RestController;
import .springframework.web.multipart.MultipartFile;

@RestController
public class controller
{
    @Autowired
    private fileUploadhelper helper;

    @GetMapping("/")
    public String hi()
    {
        return "hi!";
    }

    @PostMapping("/upload-file")
    public ResponseEntity<String> uploadFile(@RequestParam("theoneofall") MultipartFile file)
    {
        for(int i=0; i<15;i++)System.out.print('-');
        System.out.println(' ');

        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        System.out.println(file.getContentType());
        System.out.println(file.getName());

        // file upload code
        try {
            boolean f = helper.uploadFile(file);
            if(f)return ResponseEntity.ok("Successful! upload");
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong");
    }

}

upload class:

package com.mono.pgm;

import .springframework.stereotype.Component;
import .springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.InputStream;

@Component
public class fileUploadhelper
{
    public String uploadDir = "C:\\Users\\comma\\Desktop\\projects\\pgm\\src\\main\\resources\\static\\images";

    public boolean uploadFile(MultipartFile file)
    {
        boolean f = false;

        try{
            InputStream is = file.getInputStream();
            byte[] data = new byte[is.available()];
            is.read(data);

            FileOutputStream fos = new FileOutputStream(uploadDir + "\\" + file.getOriginalFilename());
            fos.write(data);
            fos.flush();
            fos.close();
            f=true;

            // Files.copy(file.getInputStream, Paths.get(uploadDir+"\\"+file.getOriginalName()),StandardCopyOption.ReplaceExisting);

        }catch(Exception e){
            e.printStackTrace();
        }
        return f;
    }
}

I asked gpt regarding it, and it told me that it might have been occurring due to some maxpostsize limit in tomcat, i tried it but didnt worked

I have been creating an endpoint to which one could upload multipart files till 50MB but is throwing the 413 request entity too large error,,

It worked for small files of 500kb but sent this error for 28MB file, but I have set the max-size to be 50MB

application.properties:

spring.application.name=pgm

# multipart config

# enable multipart uploads
spring.servlet.multipart.enabled=true
# max file-size
spring.servlet.multipart.max-file-size=50MB

logging.level..springframework.web=DEBUG
server.tomcat.max-swallow-size=50MB


The main app file:

package com.mono.pgm;

import .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PgmApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(PgmApplication.class, args);
    }
}

The controller:

package com.mono.pgm;

import .springframework.beans.factory.annotation.Autowired;
import .springframework.http.HttpStatus;
import .springframework.http.ResponseEntity;
import .springframework.web.bind.annotation.GetMapping;
import .springframework.web.bind.annotation.PostMapping;
import .springframework.web.bind.annotation.RequestParam;
import .springframework.web.bind.annotation.RestController;
import .springframework.web.multipart.MultipartFile;

@RestController
public class controller
{
    @Autowired
    private fileUploadhelper helper;

    @GetMapping("/")
    public String hi()
    {
        return "hi!";
    }

    @PostMapping("/upload-file")
    public ResponseEntity<String> uploadFile(@RequestParam("theoneofall") MultipartFile file)
    {
        for(int i=0; i<15;i++)System.out.print('-');
        System.out.println(' ');

        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        System.out.println(file.getContentType());
        System.out.println(file.getName());

        // file upload code
        try {
            boolean f = helper.uploadFile(file);
            if(f)return ResponseEntity.ok("Successful! upload");
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong");
    }

}

upload class:

package com.mono.pgm;

import .springframework.stereotype.Component;
import .springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.InputStream;

@Component
public class fileUploadhelper
{
    public String uploadDir = "C:\\Users\\comma\\Desktop\\projects\\pgm\\src\\main\\resources\\static\\images";

    public boolean uploadFile(MultipartFile file)
    {
        boolean f = false;

        try{
            InputStream is = file.getInputStream();
            byte[] data = new byte[is.available()];
            is.read(data);

            FileOutputStream fos = new FileOutputStream(uploadDir + "\\" + file.getOriginalFilename());
            fos.write(data);
            fos.flush();
            fos.close();
            f=true;

            // Files.copy(file.getInputStream, Paths.get(uploadDir+"\\"+file.getOriginalName()),StandardCopyOption.ReplaceExisting);

        }catch(Exception e){
            e.printStackTrace();
        }
        return f;
    }
}

I asked gpt regarding it, and it told me that it might have been occurring due to some maxpostsize limit in tomcat, i tried it but didnt worked

Share Improve this question asked Feb 2 at 8:18 Ayushman TripathiAyushman Tripathi 111 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There can be multiple files within one request and a request also has a maximum size which, apparently, is not automatically updated when you set a larger maximum file size. To update the maximum request size, add to your application configuration:

#  .springframework.boot.autoconfigure.web.servlet.MultipartProperties
spring.servlet.multipart:
  max-file-size: 50MB
  max-request-size: 60MB
  file-size-threshold: 64KB

(using application.yml format here, but should not be difficult to translate to properties format)

Multiparts are enabled by default as you can see in the source code, so you can skip that one.
I recommend you also take a look at the documentation for file-size-threshold. Setting a value greater than 0 can improve performance a lot (especially when dealing with lots of small files) at the cost of some extra memory usage.

发布评论

评论列表(0)

  1. 暂无评论