gpt4 book ai didi

java - Windows 上的 NIO Files.createTempDirectory 导致异常

转载 作者:行者123 更新时间:2023-12-01 09:13:50 25 4
gpt4 key购买 nike

我有一些代码应该在系统上的临时文件位置创建一个临时目录:

try {
Path tempdir = Files.createTempDirectory("tempmm");
tempdir.toFile().deleteOnExit();
tempFilename = format("%s/%s.zip", tempdir, meetId);

// Handle windows
//tempFilename = tempFilename.replace("\\", "/");

uri = URI.create("jar:file:" + tempFilename);

System.out.println("temp file uri = " + uri.toString());

} catch (IOException e) {
log.severe(format("Unable to create temporary directory: %s", e.toString()));
}

try (FileSystem zipfs = FileSystems.newFileSystem(uri, new HashMap<String, String>() {{ put("create", "true"); }})) {

Path externalMMFile = Paths.get(filePath);
Path pathInZipfile = zipfs.getPath(externalMMFile.getFileName().toString());

// copy Meet Manager Database file into the zip file
Files.copy(externalMMFile,pathInZipfile, StandardCopyOption.REPLACE_EXISTING );
} catch (IOException e) {
log.severe("Unable to create zip upload file!");
System.out.println(e.toString());
return false;
}

这是发生的异常:

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Ill
egal character in opaque part at index 11: jar:file:C:\DOCUME~1\David\LOCALS~1\T
emp\tempmm6286934818003944424/107.zip
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Ill
egal character in opaque part at index 11: jar:file:C:\DOCUME~1\David\LOCALS~1\T
emp\tempmm6286934818003944424/107.zip

有人知道我如何以独立于平台的方式做到这一点吗?

最佳答案

我发现了问题。像这样的 URI:

jar:file:C:/temp/yourmum.zip

不适用于 Windows。应该是:

jar:file:/C:/temp/yourmum.zip

所以我这样做了:

try {
Path tempdir = Files.createTempDirectory("tempmm");
tempdir.toFile().deleteOnExit();
tempFilename = format("%s" + File.separator + "%s.zip", tempdir, meetId);

if (tempFilename.contains(":\\")) {
tempFilename = "/" + tempFilename;
}

// Handle windows
tempFilename = tempFilename.replace("\\", "/");

System.out.println("tempFilename = " + tempFilename);

uri = URI.create("jar:file:" + tempFilename);

System.out.println("temp file uri = " + uri.toString());

} catch (IOException e) {
log.severe(format("Unable to create temporary directory: %s", e.toString()));
}

我不知道这是否是最好的解决方案,但它对我有用。波浪号不全是问题,问题在于分隔符。

关于java - Windows 上的 NIO Files.createTempDirectory 导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40730880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com