Java IO文件前缀字符串太短-但不是 [英] Java IO File Prefix string too short - but its not
问题描述
尝试从一个目录创建简单的.tar.gz文件.有我的代码:
Trying to create simple .tar.gz file from one directory. There is my code:
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类中的错误.这是真的 ?如何避免这个问题?
As you can see filename is correct. I googled and some people mention bug in Java IO File class. Is this true ? How to avoid this problem?
推荐答案
Checking the library code used we see that the create method look like :
public File create(String archive, File destination, File... sources) throws IOException {
...
File temp = File.createTempFile(destination.getName(), archiver.getFilenameExtension(), destination);
前缀是第一个参数.如果您检查 <代码> File.getName() 做:
The prefix is the first parameter. If you check what File.getName()
do :
返回此抽象路径名表示的文件或目录的名称.这只是路径名名称序列中的姓氏.如果路径名的名称序列为空,则返回空字符串.
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
The prefix received is too short for the temp file to create, it expect a prefixe of at least 3 character. See File.createTempFile
prefix-用于生成文件名的前缀字符串;必须至少三个字符长
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
在您的情况下,您似乎只是提供了一个文件夹,而是提供了文件名(至少3个字符).
In your case, it seems you just provide a folder, provide the file name instead (with at least 3 character).
这篇关于Java IO文件前缀字符串太短-但不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!