1 int result = 0; 2 File file = null; 3 String path = null; 4 JFileChooser fileChooser = new JFileChooser(); 5 FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句 6 System.out.println(fsv.getHomeDirectory()); //得到桌面路径 7 fileChooser.setCurrentDirectory(fsv.getHomeDirectory()); 8 fileChooser.setDialogTitle("请选择要上传的文件..."); 9 fileChooser.setApproveButtonText("确定"); 10 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 11 result = fileChooser.showOpenDialog(chatFrame); 12 if (JFileChooser.APPROVE_OPTION == result) { 13 path=fileChooser.getSelectedFile().getPath(); 14 System.out.println("path: "+path); 15 } 16 17 这是另外一种方法得到桌面路径: 18 File desktop = new File(System.getProperty("user.home")+System.getProperty("file.separator")+"XX"); 19 20 filechooser.setCurrentDirectory(desktop); 21 22 我的文档 路径: fsv.getDefaultDirectory());
原文地址:https://www.cnblogs.com/xiaoyue1606bj/p/11577256.html
时间: 2024-10-08 14:57:34