int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser(); JFileChooser
为用户选择文件提供了一种简单的机制。
FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句 //文件
System.out.println(fsv.getHomeDirectory()); //得到桌面路径
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());进入文件管理器的默认显示
fileChooser.setDialogTitle("请选择要上传的文件..."); //提示框
fileChooser.setApproveButtonText("确定"); //确定按钮
这是尤为重要的。因为JFileChooser默认的是选择文件,而需要选目录。
故要将DIRECTORIES_ONLY装入模型
另外,若选择文件,则无需此句
*/
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //设置只选择文件,或者目录. 与都可以
result = fileChooser.showOpenDialog(chatFrame); 打开文件对话框
if (JFileChooser.APPROVE_OPTION == result) {
path=fileChooser.getSelectedFile().getPath();
System.out.println("path: "+path);
}