Client-Server发送文件时对话框选择目录功能

若要实现客户端向服务器端发送文件的功能,客户端须有选择文件的按钮,服务器端须有选择储存目录、打开目录的按钮,解一下这两种做法。

首先客户端:三条语句即可

 

FileDialog filedialog = new FileDialog(new Frame(), "打开文件对话框",FileDialog.LOAD);

filedialog.setVisible(true);

filePathFiled.setText(filedialog.getDirectory()+ filedialog.getFile());//将路径填入选择文件的路径文本框

 

FileDialog (Frame parent, String title, int mode)
创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。

getDirectory()+getFile() = 文件路径

public class FileDialog extends Dialog
FileDialog 类显示出一个对话框窗口,用户可以从中选择文件。
因为它是一个模式对话框,当应用调用它的 show 方法来显示对话框时,它会阻塞应用的其余部分直到用户选择了一个文件。

 

变量索引
LOAD 该常量值表明文件对话框窗口的用途是确定从哪里读取文件。
SAVE 该常量值表明文件对话框窗口的用途是确定向哪里写入文件。

构造子索引
FileDialog(Frame) 创建一个读取文件的文件对话框。
FileDialog(Frame, String) 以指定的标题创建一个读取文件的文件对话框窗口。
FileDialog(Frame, String, int) 以指定的标题创建一个读取或保存文件的文件对话框窗口。

如果 mode 值是 LOAD ,那么文件对话框将找到文件来读取。 如果mode 值是SAVE ,那么文件对话框将找到位置来写入文件。
参数: parent - 对话框的拥有者。title - 对话框的标题。mode - 对话框的模式。

方法索引
addNotify() 创建文件对话框的同级件。
getDirectory() 获取该文件对话框的目录。
getFile() 获取该文件对话框所选中的文件。
getFilenameFilter() 确定该文件对话框的文件名过滤器。
getMode() 表示该文件对话框是用于读取文件或是用于保存文件。
paramString() 返回表示该文件对话框窗口的状态的参数字符串。
setDirectory(String) 将该文件对话框窗口的目录设置为指定的目录。
setFile(String) 将该文件对话框窗口的选定文件设置为指定的文件。
setFilenameFilter(FilenameFilter) 将该文件对话框窗口的文件名过滤器设置为指定的过滤器。
setMode(int) 设置文件对话框的模式。

如下:
public static final int LOAD 该常量值表明文件对话框窗口的用途是确定从哪里读取文件。
public static final int SAVE 该常量值表示文件对话框窗口的用途是确定向哪里写入文件。

具体事件处理

chooseButton = new JButton("\u9009\u62E9\u6587\u4EF6");
chooseButton.addActionListener(new ActionListener() {//采用匿名内部类方式,简洁便于管理
public void actionPerformed(ActionEvent e) {
FileDialog filedialog = new FileDialog(new Frame(), "打开文件对话框",
FileDialog.LOAD);
filedialog.setVisible(true);
filePathFiled.setText(filedialog.getDirectory()+ filedialog.getFile());
}
});
chooseButton.setBounds(284, 34, 93, 23);
contentPane.add(chooseButton);

服务器端:

选择目录

setSelectedFile(),返回选中的文件

JFileChooser jfc = new JFileChooser();创建一个文件选择器对象

jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

该对象设置为只选择目录

该对象调用showOpenDialog(null)会弹出对话框

选完之后,getSelectedFile()返回选中的文件

获得被选择文件的路径String path = jfc.getSelectedFile().getAbsolutePath();

显示到路径文本框this.savePath.setText(path);

 打开目录

Desktop d = Desktop.getDesktop();

d.open(new File(this.savePath.getText()));

参数为文件对象,若是目录,则启动文件管理器打开。

// 四个按钮的单击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selectButton) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置为只选择目录
int res = jfc.showOpenDialog(null);//弹出文件选择器对话框
if (res == JFileChooser.APPROVE_OPTION) {//若对话框等于选择确认后的返回值
String path = jfc.getSelectedFile().getAbsolutePath();//获得被选择文件的路径
this.savePath.setText(path);
}
} else if (e.getSource() == open) {
Desktop d = Desktop.getDesktop();
try {
d.open(new File(this.savePath.getText()));//参数为文件,若该文件是目录,则启动文件管理器打开
} catch (IOException e1) {
e1.printStackTrace();
}


} else if (e.getSource() == cleanButton) {
this.textArea.setText("");
} else {
set.sendToAll(textArea.getText() + "\n");//set对象完成向数组中的各个socket传递消息。
JOptionPane.showMessageDialog(null, "发送成功!");
}
}
}

以上。

你可能感兴趣的:(Client-Server发送文件时对话框选择目录功能)