Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在创建和使用文件后尝试删除该文件时,java.nio.file.Files.delete(path)方法会抛出java.nio.file.FileSystemException_Java_Spring Boot - Fatal编程技术网

在创建和使用文件后尝试删除该文件时,java.nio.file.Files.delete(path)方法会抛出java.nio.file.FileSystemException

在创建和使用文件后尝试删除该文件时,java.nio.file.Files.delete(path)方法会抛出java.nio.file.FileSystemException,java,spring-boot,Java,Spring Boot,我的场景如下,我从db中获取一些数据,将其写入一个文件,然后通过inputStrem将该文件提供给客户端响应。 下面是执行此操作的代码: public void getErrorsFile(UUID jobId, Pageable pageable, HttpServletResponse response) { List<ValidationErrorsDTO> failedList = getValidationErrors(jobId, pageable); S

我的场景如下,我从db中获取一些数据,将其写入一个文件,然后通过inputStrem将该文件提供给客户端响应。 下面是执行此操作的代码:

public void getErrorsFile(UUID jobId, Pageable pageable, HttpServletResponse response) {
    List<ValidationErrorsDTO> failedList = getValidationErrors(jobId, pageable);
    String fileName = "failedRows.txt";
    PrintWriter writer;
    Path path = null;
    try {
        writer = new PrintWriter(fileName, "UTF-8");
        for (ValidationErrorsDTO dto : failedList) {
            String line = dto.getLineNumber() + ": " + dto.getValidationMessage() + "\n";
            writer.println(line);
        }
        writer.flush();
        path = Paths.get(fileName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try (InputStream inputStream = Files.newInputStream(path)) {
        writeStreamToResponse(response, inputStream, fileName);
        IOUtils.closeQuietly(inputStream);
        Files.delete(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void writeStreamToResponse(HttpServletResponse response, InputStream inputStream, String fileName) throws IOException {
    response.addHeader("Content-disposition", "attachment;filename=" + fileName);
    response.setContentType("txt/plain");
    IOUtils.copy(inputStream, response.getOutputStream());
    response.flushBuffer();
}
我为解决该问题所做的SPEP:

  • 我确实将删除文件的过程放在了另一个线程中,认为这是一个并发问题
  • 我认为,没有其他进程以某种方式保存此文件
  • 我尝试了另一种删除文件的方法,使用java.io.file.delete 方法
  • 我确实把删除文件的过程放在了finally块中。 没什么帮助

  • 如果您有任何有用的建议,我们将不胜感激。

    您应该以
    的方式打开writer实例,并尝试使用类似于打开输入流的资源:

    try(writer=newprintwriter(文件名为“UTF-8”)){
    对于(ValidationErrorsDTO:failedList){
    字符串行=dto.getLineNumber()+“:”+dto.getValidationMessage()+“\n”;
    writer.println(行);
    }
    writer.flush();
    path=path.get(文件名);
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    
    看起来您没有关闭Writerwriter.flush();不是吗?是的,这绝对是问题所在,非常感谢。
    java.nio.file.FileSystemException: failedRows.txt: The process cannot access the file because it is being used by another process.