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

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

问题现象:

SpringBoot项目由1.5.x升级到2.x,文件上传启动报错


配置文件

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

报错信息

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

原因分析:

转换为Long型的数值

  /**
   * 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));
  }

通过源码分析,我们知道,文件上传的数值,需要是Long型

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


单位的英文全大写(推荐)

带单位的字符串是支持的,不过单位的英文貌似必须写全而且是全大写,比如5MB、5KB、5B(5)这样,DataSize有个parse(CharSequence text)方法

  /**
   * 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);
  }


通过源码分析,带单位的字符串是支持的,不过单位的英文貌似必须写全而且是全大写,比如5MB、5KB

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

 



相关文章
|
8月前
|
SQL 数据库
【YashanDB 知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
【YashanDB 知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
|
8月前
|
存储 数据库
【YashanDB知识库】YAS-00218 string conversion failed.
**简介:** 在数据导入导出过程中,使用 `imp` 工具导入 dump 文件时出现 `YAS-00218 string conversion failed` 错误。经分析,问题源于数据库字符集不匹配或原始 dump 文件中存在乱码。通过查看数据库服务端字符集(`show parameter character`)及采用折半查找法定位具体报错行,确认乱码导致导入失败。此问题影响版本为 23.2.1.100。
|
9月前
|
Java
java引入本地 MultipartFile 实现多部分文件上传
在Java中,`MultipartFile`通常用于处理通过HTML表单上传的文件。但在某些情况下,需要直接从本地文件系统获取文件并上传。本文介绍如何创建一个实现了`MultipartFile`接口的本地类`LocalMultipartFile`,将本地文件转换为`MultipartFile`对象,简化文件上传流程。此方法适用于批量上传等场景,避免了表单上传的复杂性。代码示例展示了如何实现和使用该类进行文件上传操作。作者:华科云商小彭。链接:[稀土掘金](https://juejin.cn/post/7377559533785530431)。
446 18
|
9月前
|
数据库
【YashanDB 知识库】exp 导出 csv 报错 YAS-00218 string conversion failed
**问题分类:** 数据导入导出 **关键字:** YAS-00218 string conversion failed **问题描述:** 使用 exp 导出成 CSV 时,报错 YAS-00218 string conversion failed。 **原因分析:** 数据库服务端字符集为 GBK,但客户端缺少 yasc_env.ini 文件,导致默认使用 UTF8 字符集。 **解决方法:** 创建 yasc_env.ini 文件并设置 CHARACTER_SET=GBK。 **影响范围:** 23.2.1.100
|
9月前
|
存储 数据库
【YashanDB 知识库】YAS-00218 string conversion failed.
**简介:** 在使用 `imp` 导入 dump 文件时,遇到错误 YAS-00218 string conversion failed。通过检查数据库字符集及使用折半查找法定位问题行,发现原始 dump 文件中存在乱码,导致导入失败。此问题影响版本 23.2.1.100。建议检查并修正源数据字符编码后再进行导入操作。
|
9月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
621 7
|
8月前
|
SQL 数据库
【YashanDB知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
【YashanDB知识库】导入数据时报错:YAS-00008 type convert error:literal does not match format string
|
存储 前端开发 Java
Java后端如何进行文件上传和下载 —— 本地版(文末配绝对能用的源码,超详细,超好用,一看就懂,博主在线解答) 文件如何预览和下载?(超简单教程)
本文详细介绍了在Java后端进行文件上传和下载的实现方法,包括文件上传保存到本地的完整流程、文件下载的代码实现,以及如何处理文件预览、下载大小限制和运行失败的问题,并提供了完整的代码示例。
5027 2
|
Java
java 文件上传和下载
java 文件上传和下载
99 0
|
Java 编译器
Error:java: Compilation failed: internal java compiler error
Error:java: Compilation failed: internal java compiler error
1997 1