在我的端点中,我接收Base64格式的字符串形式的文件内容. 解压在Seven Zip库上运行得很好,没有问题. 但在执行完这行代码后
archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(
new RandomAccessFile(file, "r")));
Java无法删除文件,原因如下:
java.nio.file.FileSystemException: myFile.RAR: The process cannot access the file because it is being used by another process
我曾try 使用Junrar库,但Seven Zip运行得更好(支持RAR5,并将较少损坏的文件识别为Junrar).
-pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding</artifactId>
<version>16.02-2.01</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding-all-platforms</artifactId>
<version>16.02-2.01</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
以下是dto:
public record UploadDto(
@NotNull
String name,
@NotNull
String data) {
}
以下是带有逻辑的控制器:
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
@RestController
public class TestController {
private final Logger log = LoggerFactory.getLogger(TestController.class);
@PostMapping("/upload")
public void extractRar(@RequestBody UploadDto dto){
byte[] decodedBytes = Base64.getDecoder().decode(dto.data());
try {
String filePath = dto.name();
Files.write(Paths.get(filePath), decodedBytes);
log.info("File saved successfully to: " + filePath);
File file = new File(filePath);
IInArchive archive;
try {
archive = SevenZip.openInArchive(null, new RandomAccessFileInStream(
new RandomAccessFile(file, "r")));
log.info(String.valueOf(archive.getNumberOfItems()));
} catch (SevenZipException | FileNotFoundException e) {
throw new RuntimeException(e);
}
archive.close();
// 1 method
Files.delete(Path.of(filePath));
// 2 method
if (file.delete()) {
log.info("file deleted successfully");
} else {
log.error("failed to delete the temporary file used for unzipping");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
请求正文示例:
{
"name": "myFile.RAR",
"data": "UmFyIRoHAM+QcwAADQAAAAAAAABTL3QgkC8AFAAAABQAAAACTeXQiWmKL1cdMAoAIAAAAG15RmlsZS50eHQAsAA/PXJhciBkYXRhIG1vY2sgaGVyZQ0KxD17AEAHAA=="
}
我try 了两种删除文件的方法,但似乎都不起作用. 此外,我也没有找到任何使用字节数组或InputStream打开存档的方法. 如果我可以在提取内容后删除文件,或者在不将文件保存在本地的情况下提取内容,问题就会得到解决. 如有任何帮助或 idea ,我们将不胜感激! 操作系统Windows 10