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.io.File转换为java.nio.File?_Java_Spring_Image_Spring Boot_File - Fatal编程技术网

如何将此方法从使用java.io.File转换为java.nio.File?

如何将此方法从使用java.io.File转换为java.nio.File?,java,spring,image,spring-boot,file,Java,Spring,Image,Spring Boot,File,基本上,我有一个从教程中得到的方法(我的主要目标是简单地从spring引导服务器返回图像,这样我就可以动态地查看它们) 我一直在四处搜索,注意到人们建议使用java.nio.file,但我对如何在这里实现这一点有点迷茫。感谢您的帮助。首先获取文件夹的路径: Path folderPath = Paths.get(filesPath); 如果您的路径指向一个目录,您可以使用文件获取其内容的流。列表: if (Files.isDirectory(folderPath)) { List<

基本上,我有一个从教程中得到的方法(我的主要目标是简单地从spring引导服务器返回图像,这样我就可以动态地查看它们)


我一直在四处搜索,注意到人们建议使用
java.nio.file
,但我对如何在这里实现这一点有点迷茫。感谢您的帮助。

首先获取文件夹的
路径:

Path folderPath = Paths.get(filesPath);
如果您的
路径
指向一个目录,您可以使用
文件获取其内容的
流。列表

if (Files.isDirectory(folderPath)) {
    List<Path> files = Files.list(folderPath)
         .filter(path -> !Files.isDirectory(path))
         .collect(Collectors.toList());

    // Do something with the files.
}
if(Files.isDirectory(folderPath)){
列表文件=文件。列表(folderPath)
.filter(路径->!Files.isDirectory(路径))
.collect(Collectors.toList());
//对这些文件做些什么。
}

看起来您没有使用
FileInputStream
进行任何操作,因此不需要翻译该部分。要获取路径的文件扩展名,您可能需要将
路径
转换为字符串,然后自己提取扩展名。

首先获取文件夹的
路径

Path folderPath = Paths.get(filesPath);
如果您的
路径
指向一个目录,您可以使用
文件获取其内容的
流。列表

if (Files.isDirectory(folderPath)) {
    List<Path> files = Files.list(folderPath)
         .filter(path -> !Files.isDirectory(path))
         .collect(Collectors.toList());

    // Do something with the files.
}
if(Files.isDirectory(folderPath)){
列表文件=文件。列表(folderPath)
.filter(路径->!Files.isDirectory(路径))
.collect(Collectors.toList());
//对这些文件做些什么。
}
看起来您没有使用
FileInputStream
进行任何操作,因此不需要翻译该部分。要获取路径的文件扩展名,您可能需要将
路径
转换为字符串,然后自己提取扩展名。

nio示例:

  public List<String> readImages() throws IOException {
    return Files.list(Path.of("/images"))
            .filter(Files::isRegularFile)
            .map(this::encode)
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
  }

  private String encode(Path file) {
    try {
      String extension = FilenameUtils.getExtension(file.getFileName().toString());
      String encodeBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(file));
      return "data:image/"+extension+";base64,"+encodeBase64;
    } catch (Exception e) {
      return null;
    }
  }
public List readImages()引发IOException{
返回Files.list(路径(“/images”))
.filter(文件::isRegularFile)
.map(this::encode)
.filter(对象::非空)
.collect(Collectors.toList());
}
私有字符串编码(路径文件){
试一试{
字符串扩展名=FilenameUtils.getExtension(file.getFileName().toString());
字符串encodeBase64=Base64.getEncoder().encodeToString(Files.readAllBytes(file));
返回“数据:图像/”+扩展名+“base64”+encodeBase64;
}捕获(例外e){
返回null;
}
}
nio示例:

  public List<String> readImages() throws IOException {
    return Files.list(Path.of("/images"))
            .filter(Files::isRegularFile)
            .map(this::encode)
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
  }

  private String encode(Path file) {
    try {
      String extension = FilenameUtils.getExtension(file.getFileName().toString());
      String encodeBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(file));
      return "data:image/"+extension+";base64,"+encodeBase64;
    } catch (Exception e) {
      return null;
    }
  }
public List readImages()引发IOException{
返回Files.list(路径(“/images”))
.filter(文件::isRegularFile)
.map(this::encode)
.filter(对象::非空)
.collect(Collectors.toList());
}
私有字符串编码(路径文件){
试一试{
字符串扩展名=FilenameUtils.getExtension(file.getFileName().toString());
字符串encodeBase64=Base64.getEncoder().encodeToString(Files.readAllBytes(file));
返回“数据:图像/”+扩展名+“base64”+encodeBase64;
}捕获(例外e){
返回null;
}
}

我用这个代码解决了这个问题:

@Autowired
    ServletContext context;
    
    @GetMapping(path = "/allImages")
    public List<String> readImages() throws IOException {
        return Files.list(Paths.get(context.getRealPath("/images")))
                .filter(Files::isRegularFile)
                .map(this::encode)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
      }
    
    private String encode(Path file) {
        try {
          String extension = FilenameUtils.getExtension(file.getFileName().toString());
          String encodeBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(file));
          return "data:image/"+extension+";base64,"+encodeBase64;
        } catch (Exception e) {
          return null;
        }
      }
@Autowired
ServletContext;
@GetMapping(路径=“/allImages”)
public List readImages()引发IOException{
返回Files.list(path.get(context.getRealPath(“/images”))
.filter(文件::isRegularFile)
.map(this::encode)
.filter(对象::非空)
.collect(Collectors.toList());
}
私有字符串编码(路径文件){
试一试{
字符串扩展名=FilenameUtils.getExtension(file.getFileName().toString());
字符串encodeBase64=Base64.getEncoder().encodeToString(Files.readAllBytes(file));
返回“数据:图像/”+扩展名+“base64”+encodeBase64;
}捕获(例外e){
返回null;
}
}

感谢所有帮助过我的人。

我用以下代码解决了这个问题:

@Autowired
    ServletContext context;
    
    @GetMapping(path = "/allImages")
    public List<String> readImages() throws IOException {
        return Files.list(Paths.get(context.getRealPath("/images")))
                .filter(Files::isRegularFile)
                .map(this::encode)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
      }
    
    private String encode(Path file) {
        try {
          String extension = FilenameUtils.getExtension(file.getFileName().toString());
          String encodeBase64 = Base64.getEncoder().encodeToString(Files.readAllBytes(file));
          return "data:image/"+extension+";base64,"+encodeBase64;
        } catch (Exception e) {
          return null;
        }
      }
@Autowired
ServletContext;
@GetMapping(路径=“/allImages”)
public List readImages()引发IOException{
返回Files.list(path.get(context.getRealPath(“/images”))
.filter(文件::isRegularFile)
.map(this::encode)
.filter(对象::非空)
.collect(Collectors.toList());
}
私有字符串编码(路径文件){
试一试{
字符串扩展名=FilenameUtils.getExtension(file.getFileName().toString());
字符串encodeBase64=Base64.getEncoder().encodeToString(Files.readAllBytes(file));
返回“数据:图像/”+扩展名+“base64”+encodeBase64;
}捕获(例外e){
返回null;
}
}

感谢所有帮助过您的人。

您可能需要验证
filepath
是否指向f.x所期望的目录。记录它。验证它后,路径正确指向图像文件夹您可能需要验证
filepath
是否指向f.x所期望的目录。记录它。验证它,路径正确地指向图像folderI get
java.nio.file.NoSuchFileException:\images
在方法运行后。我在方法运行后获得
java.nio.file.NoSuchFileException:\images
。因此folderPath具有到图像的正确路径。但是,由于某种原因,即使我导入了nio,isDirectory()也没有解析。@很抱歉,它应该是
文件。isDirectory(路径)
。现在已修复。因此folderPath具有图像的正确路径。但是,由于某种原因,即使我导入了nio,isDirectory()也没有解析。@很抱歉,它应该是
文件。isDirectory(路径)
。现在修好了。