使用 Java FTPClient 上传时,文件获取本地源文件的完整路径作为其名称

File gets a full path of local source file as its name when uploaded using Java FTPClient

所以我有一个基本的 GUI 应用程序,其中有一个选项可以将图像文件上传到 ftp 服务器。一切正常,除了一件事:文件在上传过程中被重命名。文件的新名称将是包含该文件的目录的完整路径。

因此,就我而言,桌面上有一张图片:C:\Users\Bob\Desktop\image.png。当我select JfileChooser 中的文件时,名称仍然只是image.png。但是当我点击上传到 FTP 服务器时,文件将被重命名为 C:\Users\Bob\Desktop\image.png。所以如果我想下载那个文件,我必须使用这个路径:/home/user/users/xy/images/C:\Users\Bob\Desktop\image.png 才能下载它。不知道是什么导致了这个问题。我使用 FTPClient.putFileToPath(file,path) 上传文件,它工作正常,文件将被上传。我试图用 total commander 从我的机器复制一个文件到 ftp 服务器,但这个问题从未发生过。我提供了一些代码片段,它完成了上传工作。

uploadmenu.getUploadBtn().addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(!uploadMenuValidate()){
           for(File f : img_container){
            try {
                //This still gives me the normal file name
                System.out.println(f.getName()); 
                
                ftp.putFileToPath(f, FtpClient.DEST_DIR+SQLData.APP_USERNAME+"/"+f);

                 
            } catch (IOException ex) {
                ex.printStackTrace();
                }
            }
           popup.setVisible(false);
        }
    }
});

我在 JFileChooser 中编辑的 img_container 数组中有所有文件。

File.toString() returns:

Returns the pathname string of this abstract pathname

您想使用 File.getName():

ftp.putFileToPath(f, FtpClient.DEST_DIR+SQLData.APP_USERNAME+"/"+f.getName());