获取java.io.IOException:间歇性地没有这样的文件或目录

时间:2018-04-16 19:02:38

标签: java linux ioexception

我有一个春季预定的工作,每分钟运行一次。每当预定义的标准匹配时,它就会跟随。

  1. 创建目录
  2. 在循环中将一堆文件写入其中
  3. 拉链
  4. 将拉链复制到另一个位置
  5. 删除目录
  6. 上面的Psuedo代码。

    // id is numeric primary key. so it is guarenteed to be unique and contains no special characters
    final List<AttachmentDTO> attachmentDtos = service.getAttachments(id);
    
    if (!attachmentDtos.isEmpty()) {
        final File directoryFolder = new File(properties.getZIPFolder() + fileSeperator+ id);
    
        // create directory
        if (!directoryFolder.exists()) {
            logger.debug("Creating directory : {}", directoryFolder);
            final boolean isDirectoryCreated = directoryFolder.mkdir();
            logger.debug("Directory: {}, created : {}", directoryFolder, isDirectoryCreated);
            }
    
        // write files into directory
        for (final AttachmentDTO dto : attachmentDtos) {
            final byte[] dataFromGeneva = util.getStream(dto.getFileKey);
            final String fileName = dto.getFilename();
    
            final File file = new File(directoryFolder, fileName);
            logger.debug("getAbsolutePath ={}",file.getAbsolutePath());
    
            if (!file.exists()) {
                final boolean isFileCreated = file.createNewFile();
                logger.info("Is new file created : {}", isFileCreated);
            }
    
            try (FileOutputStream fostream = new FileOutputStream(file)) {
                IOUtils.write(data, fostream);
                fostream.flush();
            }
        }
    
        // Zip the directory
        final String zipFilePath = FileTransferUtil.writeZipFile(directoryFolder, properties.getZIPFileFolder());
        FileTransferUtil.copyZip(zipFilePath);
        FileUtils.deleteDirectory(directoryFolder);
    }
    

    这段代码已经很好地工作了很长时间,但最近开始出现问题。在for循环中,它适用于少数文件,然后突然抛出IOException。

    示例日志,

    2018-04-16 12:17:39,385 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:250) : Creating directory : /app/sharedPath/zipfilefolder/125201
    2018-04-16 12:17:39,386 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:252) : Directory: /app/sharedPath/zipfilefolder/125201, created : true
    2018-04-16 12:17:40,306 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 1.pdf
    2018-04-16 12:17:40,306 - INFO - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:272) : Is new file created : true
    2018-04-16 12:17:40,442 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 2.pdf
    2018-04-16 12:17:40,442 - INFO - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:272) : Is new file created : true
    2018-04-16 12:17:40,793 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 3.pdf
    2018-04-16 12:17:40,794 - ERROR - [pool-1-thread-1] com.test.sp.jobs.helper.SPJobHelper.handleError(SPJobHelper.java:80) : errMsg=java.io.IOException: No such file or directory
        at java.io.UnixFileSystem.createFileExclusively(Native Method)
        at java.io.File.createNewFile(File.java:1012)
        at com.test.Test.downloadAttachments(Test.java:271)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    
    1. 如果它是一个路径或访问问题,那么它将不适用于第一个 几次迭代。
    2. 我想因为文件名中有空格 “样本文件1.pdf”,这可能会弄乱路径。但那是不真实的。 它似乎对其他迭代和重试工作得很好。
    3. 这里没有并发性;只有1个线程在该目录上写入,读取和删除。
    4. 在预定作业失败后,我们会重置DB中的条件,以便再次获取它。这次所有文件都通过了。所以它看起来不像文件参数的问题。
    5. 作业在linux机器上运行。

      我遇到了类似的问题,但没有找到任何有用的东西。甚至试图找到UnixFileSystem.createFileExclusively()的本机实现。我不清楚为什么系统会间歇性地抛出这个IOException。希望有人能指出这里缺少的东西。谢谢!

0 个答案:

没有答案