在开发和部署Spring Boot应用时,打包和运行过程中可能会遇到各种问题,尤其是当项目涉及特殊需求(如中文文件名处理)或打包配置不当时。本文将通过一个实际案例,详细分析常见的打包和运行问题,提供解决方案,并总结最佳实践。
用户在使用Spring Boot开发一个文件处理应用时,遇到以下两个主要问题:
MALFORMED。java -jar启动时提示no main manifest attribute,手动指定主类后,又报NoClassDefFoundError。ZipInputStream默认使用的编码可能与ZIP文件的实际编码不一致,导致中文文件名解析错误。优化unzipFile方法,正确处理编码和路径安全:
private List<File> unzipFile(MultipartFile file) throws IOException {
List<File> extractedFiles = new ArrayList<>();
File tempDir = Files.createTempDirectory("unzip_").toFile();
tempDir.deleteOnExit();
// 尝试GBK编码(常见于Windows生成的ZIP)
try (ZipInputStream zipIn = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"))) {
ZipEntry entry;
while ((entry = zipIn.getNextEntry()) != null) {
String entryName = entry.getName();
File destFile = new File(tempDir, entryName);
// 安全检查:防止ZIP路径遍历
String canonicalPath = destFile.getCanonicalPath();
if (!canonicalPath.startsWith(tempDir.getCanonicalPath() + File.separator)) {
throw new SecurityException("ZIP文件包含非法路径: " + entryName);
}
if (!entry.isDirectory() && isImageFile(entryName)) {
Files.createDirectories(destFile.getParentFile().toPath());
Files.copy(zipIn, destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
extractedFiles.add(destFile);
}
zipIn.closeEntry();
}
} catch (Exception e) {
// 如果GBK失败,回退到UTF-8
try (ZipInputStream zipIn = new ZipInputStream(file.getInputStream(), StandardCharsets.UTF_8)) {
// 重复解压逻辑...
}
}
return extractedFiles;
}getCanonicalPath检查路径合法性,防止恶意ZIP文件攻击。Files.copy替代手动缓冲读写,更高效可靠。MANIFEST.MF中的Main-Class。BOOT-INF/lib缺失)。修正pom.xml,确保正确打包:
<build>
<plugins>
<!-- 1. 指定Java编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 2. 关键:正确配置Spring Boot Maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal> <!-- 生成可执行JAR -->
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.debang.debang_phone_tool.DebangPhoneToolApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>移除<skip>true</skip>:确保插件执行重新打包操作。
明确指定mainClass:避免运行时找不到主类。
验证打包结果:
# 检查JAR结构
jar tf target/debang_phone_tool-0.0.1-SNAPSHOT.jar | grep BOOT-INF/lib
# 检查MANIFEST.MF
jar xf target/debang_phone_tool-0.0.1-SNAPSHOT.jar META-INF/MANIFEST.MF && cat META-INF/MANIFEST.MFsrc/
├── main/
│ ├── java/
│ │ └── com/debang/debang_phone_tool/
│ │ ├── DebangPhoneToolApplication.java # Spring Boot主类
│ │ ├── controller/ # 控制器
│ │ └── service/ # 业务逻辑
│ └── resources/
│ ├── static/ # 静态文件
│ └── application.yml # 配置文件
pom.xml # 修正后的Maven配置# 清理并重新打包
mvn clean package
# 后台运行(Linux)
nohup java -jar target/debang_phone_tool-0.0.1-SNAPSHOT.jar > app.log 2>&1 &
# 查看日志
tail -f app.loggetCanonicalPath检查路径合法性。必须使用spring-boot-maven-plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>验证JAR结构:
BOOT-INF/lib(依赖库)和META-INF/MANIFEST.MF。避免<skip>true</skip>:否则会导致依赖未打包。
日志重定向:使用2>&1捕获所有输出:
nohup java -jar app.jar > app.log 2>&1 &进程管理:结合systemd或supervisord实现服务化。
通过本文的分析与解决方案,我们解决了Spring Boot项目中的两个典型问题:
希望这些经验能帮助你避免类似问题,提升Spring Boot项目的开发和部署效率!