SpingBoot2.x文件上传:failed to convert java.lang.String to org.springframework.util.unit.DataSize

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/fly910905/article/details/90598418

Symptoms:

SpringBoot project upgrade from 1.5.x to 2.x, start file upload error

Profiles

#文件上传路径 linux配置路径
spring.servlet.multipart.max-file-size=50Mb
spring.servlet.multipart.max-request-size=50Mb
file.upload.realpath=D:/csizgcardmanage/files

Error Messages

Failed to bind properties under 'spring.servlet.multipart.max-request-size' to org.springframework.util.unit.DataSize:

    Property: spring.servlet.multipart.max-request-size
    Value: 50Mb
    Origin: class path resource [application-dev.properties]:36:43
    Reason: failed to convert java.lang.String to org.springframework.util.unit.DataSize

Action:

Update your application's configuration

Cause Analysis:

Long type value is converted to

	/**
	 * Obtain a {@link DataSize} representing the specified number of megabytes.
	 * @param megabytes the number of megabytes, positive or negative
	 * @return a {@link DataSize}
	 */
	public static DataSize ofMegabytes(long megabytes) {
		return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB));
	}

Through source code analysis, we know that the file upload values, needs Long type

spring.servlet.multipart.max-file-size=50000000

English units of all uppercase (recommended)

String with units that support, but the units must be written in English seemingly full but for all uppercase, such as 5MB, 5KB, 5B (5) In this way, DataSize there parse (CharSequence text) method

	/**
	 * Obtain a {@link DataSize} from a text string such as {@code 12MB} using
	 * {@link DataUnit#BYTES} if no unit is specified.
	 * <p>
	 * Examples:
	 * <pre>
	 * "12KB" -- parses as "12 kilobytes"
	 * "5MB"  -- parses as "5 megabytes"
	 * "20"   -- parses as "20 bytes"
	 * </pre>
	 * @param text the text to parse
	 * @return the parsed {@link DataSize}
	 * @see #parse(CharSequence, DataUnit)
	 */
	public static DataSize parse(CharSequence text) {
		return parse(text, null);
	}

By analyzing source code, with a string of units are supported, but the unit must be written in English, seemingly full but for all uppercase, such as 5MB, 5KB

spring.servlet.multipart.max-file-size=50MB

 

Guess you like

Origin blog.csdn.net/fly910905/article/details/90598418