问题描述:

In my program, I want to sent some files from client to server using socket programming. I'm using the void setMultiSelectionEnabled(boolean b) method so I can choose more than 1 file, but it gives me an error when i try to get the path of the file. Here's my code:

 JFileChooser choose = new JFileChooser();

choose.setAcceptAllFileFilterUsed(false);

choose.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "jpeg"));

choose.setAcceptAllFileFilterUsed(true);

choose.setMultiSelectionEnabled(true);

File[] f = choose.getSelectedFiles();

choose.showOpenDialog(this);

String filePath = f.getAbsolutePath();

String fname = f.getName();

Client_ftp cli = new Client_ftp();

if(cli.kirim(filePath, fname)) {

jLabel1.setText("Success. .");

} else {

jLabel1.setText("failed");

}

网友答案:

f is an array. You'll need to index the array before calling the getAbsolutePath() method for each File in f. Make sure to check for null prior to doing so.

Example: f[0].getAbsolutePath();

网友答案:

f.getSelectedFile().getAbsolutePath()

So edit to add some explanation:

You can't get the absolute path of the file chooser.. You have to use the getSelectedFile(s)() method to get the path of the selected files. I hope it helped you :)

相关阅读:
Top