Java IO 文件前缀字符串太短 - 但不是

Tee*_*emo 2 java file

试图从一个目录创建简单的 .tar.gz 文件。有我的代码:

File destinationFile = new File("/var/www/swOfflineFeeds/Companies/2/")
File sourceFile = new File("/var/www/swOfflineFeeds/Companies/2/64cacf30-b294-49f4-b166-032a808d73cd/")
println("destinationFile exists: " + destinationFile.exists()) //prints true
println("sourceFile exists: " + sourceFile.exists()) //prints true

Archiver arch = ArchiverFactory.createArchiver(ArchiveFormat.TAR, CompressionType.GZIP)
File archiveFile = arch.create("64cacf30-b294-49f4-b166-032a808d73cd", destinationFile, sourceFile)
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

| Error 2018-02-15 12:47:08,925 [http-bio-8183-exec-1] ERROR errors.GrailsExceptionResolver  - IllegalArgumentException occurred when processing request: [GET] /socialwall/test/index
Prefix string too short. Stacktrace follows:
Message: Prefix string too short
    Line | Method
->> 1978 | createTempFile in java.io.File
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     51 | create         in org.rauschig.jarchivelib.ArchiverCompressorDecorator
|     19 | index . . . .  in com.manas.socialwall.TestController$$EQjisHuy
|    198 | doFilter       in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter . . . in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker      in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run            in java.lang.Thread
Run Code Online (Sandbox Code Playgroud)

如您所见,文件名是正确的。我用谷歌搜索,有些人提到 Java IO File 类中的错误。这是真的 ?如何避免这个问题?

Axe*_*elH 6

检查使用的库代码,我们看到 create 方法如下所示:

public File create(String archive, File destination, File... sources) throws IOException {
     ...
     File temp = File.createTempFile(destination.getName(), archiver.getFilenameExtension(), destination);
Run Code Online (Sandbox Code Playgroud)

前缀是第一个参数。如果你检查做File.getName()什么:

返回此抽象路径名表示的文件或目录的名称。这只是路径名名称序列中的最后一个名称。如果路径名的名称序列为空,则返回空字符串。

在你的情况下。

File destinationFile = new File("/var/www/swOfflineFeeds/Companies/2/");
System.out.println(destinationFile.getName());
Run Code Online (Sandbox Code Playgroud)

2

收到的前缀太短,临时文件无法创建,它需要至少 3 个字符的前缀。看File.createTempFile

prefix - 用于生成文件名的前缀字符串;长度必须至少为三个字符

在您的情况下,您似乎只提供一个文件夹,而是提供文件名(至少包含 3 个字符)。


迈克尔创建一个问题