当前位置: 代码网 > it编程>编程语言>Java > springboot 打包 failed with MalformedInputException: Input length=1

springboot 打包 failed with MalformedInputException: Input length=1

2025年07月10日 Java 我要评论
malformedinputexception: input length = 1 是 spring boot 项目在打包时常见的错误,通常是由于资源文件(如 .properties、.yml 或其他

malformedinputexception: input length = 1 是 spring boot 项目在打包时常见的错误,通常是由于资源文件(如 .properties.yml 或其他文本文件)的编码问题导致的。maven 在读取这些文件时,如果文件的编码与预期的编码(通常是 utf-8)不匹配,就会抛出此异常。

以下是解决该问题的详细步骤:

1.检查文件编码

  • 确保 src/main/resources 目录下的所有文件都使用 utf-8 编码
  • 如果你使用的是 ide(如 intellij idea 或 eclipse),可以按照以下步骤检查和修改文件编码:
    • intellij idea:
      1. 打开 file > settings > editor > file encodings
      2. global encodingproject encodingdefault encoding for properties files 都设置为 utf-8
      3. 重新保存文件。

2.在pom.xml中配置编码(我在这一步就解决了问题)

pom.xml 中明确指定资源文件的编码为 utf-8。修改 <build> 部分如下:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering> <!-- 如果需要过滤 -->
            <encoding>utf-8</encoding>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupid>org.apache.maven.plugins</groupid>
            <artifactid>maven-resources-plugin</artifactid>
            <version>3.3.1</version> <!-- 使用最新版本 -->
            <configuration>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

这样可以确保 maven 在打包时使用 utf-8 编码处理资源文件。

3.排除非 utf-8 编码的文件

如果某些文件无法转换为 utf-8 编码(例如二进制文件),可以在 pom.xml 中排除这些文件:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>**/*.bin</exclude> <!-- 示例:排除二进制文件 -->
        </excludes>
        <filtering>true</filtering>
        <encoding>utf-8</encoding>
    </resource>
</resources>

4.禁用过滤

如果问题是由于资源过滤(例如 ${variable} 占位符)引起的,可以尝试禁用过滤:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering> <!-- 禁用过滤 -->
        <encoding>utf-8</encoding>
    </resource>
</resources>

如果只需要对某些文件启用过滤,可以单独配置:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/*.properties</include> <!-- 仅对 properties 文件启用过滤 -->
        </includes>
        <encoding>utf-8</encoding>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering> <!-- 对其他文件禁用过滤 -->
        <excludes>
            <exclude>**/*.properties</exclude>
        </excludes>
        <encoding>utf-8</encoding>
    </resource>
</resources>

5.转换文件编码

如果某些文件不是 utf-8 编码,可以使用工具将其转换为 utf-8:

  • 使用文本编辑器(如 notepad++ 或 vs code)打开文件,然后选择 save with encoding > utf-8
  • 使用命令行工具 iconv 转换编码:
    iconv -f iso-8859-1 -t utf-8 inputfile.properties -o outputfile.properties
    

6.检查特殊字符

如果资源文件中包含特殊字符(如非 ascii 字符),确保它们被正确转义。例如,在 .properties 文件中,可以使用 unicode 转义序列:

greeting=\u4f60\u597d

7.更新 maven 和插件版本

确保你使用的是最新版本的 maven 和 maven-resources-plugin。在 pom.xml 中更新插件版本:

<properties>
    <maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupid>org.apache.maven.plugins</groupid>
            <artifactid>maven-resources-plugin</artifactid>
            <version>${maven-resources-plugin.version}</version>
        </plugin>
    </plugins>
</build>

8.清理并重新构建

完成上述修改后,运行以下命令清理并重新构建项目:

mvn clean install

9.调试模式

如果问题仍然存在,可以使用 maven 的调试模式查看详细错误信息:

mvn clean install -x

检查日志中是否有具体的文件路径和行号,找到导致问题的文件。

通过以上步骤,你应该能够解决 malformedinputexception: input length = 1 的问题。如果问题仍未解决,请提供更多错误日志或配置信息,我可以进一步帮助你排查问题!

到此这篇关于springboot 打包 failed with malformedinputexception: input length=1的文章就介绍到这了,更多相关springboot 打包错误内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com