Java IO 文件前缀字符串太短 - 但事实并非如此

标签 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)

我收到错误消息:

| 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

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

最佳答案

检查library code使用我们看到创建方法如下所示:

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

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

Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence. If the pathname's name sequence is empty, then the empty string is returned.

就你而言。

File destinationFile = new File("/var/www/swOfflineFeeds/Companies/2/");
System.out.println(destinationFile.getName());

2

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

prefix - The prefix string to be used in generating the file's name; must be at least three characters long

就您而言,您似乎只提供了一个文件夹,而是提供了文件名(至少 3 个字符)。

<小时/>

An issue has been created作者:迈克尔。

关于Java IO 文件前缀字符串太短 - 但事实并非如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48806562/

相关文章:

node.js - 使用 mongoose 在 mongodb 中存储文件

java - 搜索整个C盘的文件类型

java - 在条款中使用 createNativeQuery

java - 更新 ListView 中的按钮

regex - 通过bash将一个文件夹的某些文件分类到另一个文件中

c - 使用 fgets() 从文件复制到字符串的段错误

c++ - 如何读取 .avi 文件 C++

java - Spring RestTemplate 无法解码包含 "&lt;!DOCTYPE ... dtd>"的 XML

java - 奇怪的 Hazelcast IMap#put() 行为

java - 基于URL参数缓存JSP