You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将JFileChooser选择的图片保存至Java项目目录?

问题

现有以下用于选择图片文件的代码:

public void run() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "png"));
    while (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        try {
            File f = fileChooser.getSelectedFile();  
            BufferedImage thisImage = ImageIO.read(f);
        }
    }
}

需要将选中的图片保存到项目根目录中,尝试过ImageIO.write()及以下两种方法但均无效:
方法1:

Files.copy(f.toPath(), new File(System.getProperty("user.dir")).toPath(), StandardCopyOption.REPLACE_EXISTING);

方法2:

ImageIO.write(thisImage, "png", f); 
解决方案

问题分析

  1. 方法1错误原因:new File(System.getProperty("user.dir"))指向项目根目录文件夹,Files.copy无法直接将文件复制到文件夹路径,必须指定具体的目标文件名。
  2. 方法2错误原因:f是用户选中的源文件路径,执行后仅覆盖原文件,并未写入到项目目录。

正确实现方式

方式一:使用ImageIO.write()写入项目目录

获取项目根目录后,拼接原文件名生成目标文件路径,再调用ImageIO.write()完成保存:

public void run() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "png"));
    // 获取项目根目录
    String projectDir = System.getProperty("user.dir");
    
    while (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        try {
            File f = fileChooser.getSelectedFile();  
            BufferedImage thisImage = ImageIO.read(f);
            
            // 提取原文件名,生成项目目录下的目标文件
            String fileName = f.getName();
            File targetFile = new File(projectDir, fileName);
            
            // 获取原文件格式(避免强制转成png导致格式丢失)
            String format = fileName.substring(fileName.lastIndexOf(".") + 1);
            // 写入图片到项目目录
            if (ImageIO.write(thisImage, format, targetFile)) {
                System.out.println("图片已保存到项目目录:" + targetFile.getAbsolutePath());
            } else {
                System.out.println("不支持的图片格式,保存失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方式二:使用Files.copy()复制到项目目录

同样需要指定完整的目标文件路径,而非文件夹:

public void run() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "png"));
    String projectDir = System.getProperty("user.dir");
    
    while (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        try {
            File f = fileChooser.getSelectedFile();  
            // 生成项目目录下的目标文件路径
            File targetFile = new File(projectDir, f.getName());
            // 复制文件到项目目录,替换已存在的同名文件
            Files.copy(f.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            System.out.println("图片已复制到项目目录:" + targetFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  • System.getProperty("user.dir")获取的是项目运行时的工作目录,在IDE中通常为项目根目录;若为打包后的jar,路径则是jar所在的目录。
  • 若需自定义保存的文件名,直接修改targetFile的文件名部分即可。

内容的提问来源于stack exchange,提问作者Kai Kishpaugh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 06:46:08