【问题标题】:Uploading blank file when FTP path has more than one subdirectoryFTP路径有多个子目录时上传空白文件
【发布时间】:2012-07-07 16:39:12
【问题描述】:

以下代码将文件上传到 FTP 服务器

public class UploadFile {
    static ResourceBundle rsBundle = ResourceBundle.getBundle("com.mindcraft.resources.resources");
    public void upload(String host,String username,String pwd,String inputfile,String uploadpath,String zip_filename)
    {      
        //String zip_file_name= rsBundle.getString("zip_filename");
        FTPClient ftp=new FTPClient();
        try {          
            int reply;  
            ftp.connect(host); 
            ftp.login(username, pwd); 
            reply = ftp.getReplyCode();
            System.out.println("reply1" + reply);
            if(!FTPReply.isPositiveCompletion(reply)) 
            {             
                ftp.disconnect();           

            }           
            System.out.println("FTP server connected."); 
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            InputStream input= new FileInputStream(inputfile);

            System.out.println("Directory.." + ftp.printWorkingDirectory());

            String dirTree=uploadpath;
            boolean dirExists = true;
            String[] directories = dirTree.split("/");
            for (String dir : directories )
            {
                if (!dir.isEmpty() )
                {
                    if (dirExists)
                    {
                        dirExists = ftp.changeWorkingDirectory(dir);
                        ftp.storeFile(dirTree+zip_filename,input);
                        System.out.println("1..");
                    }
                    if (!dirExists)
                    {

                        System.out.println("dir tree" + ftp.printWorkingDirectory());


                        if (!ftp.makeDirectory(dir))
                        {

                            throw new IOException("Unable to create remote directory '" + dir + "'.  error='" + ftp.getReplyString()+"'");
                            }
                        if (!ftp.changeWorkingDirectory(dir))
                        {

                            throw new IOException("Unable to change into newly created remote directory '" + dir + "'.  error='" + ftp.getReplyString()+"'");
                        }
                        System.out.println("dir tree" + ftp.printWorkingDirectory());
                        ftp.storeFile(dirTree+zip_filename,input);
                    }

                    }
                }      

            System.out.println( ftp.getReplyString() );
            input.close();
            ftp.logout();        
            } 
        catch(Exception e) 
            {                      
            System.out.println("err"+ e);          
            e.printStackTrace();     
            }
        finally 
        {
            if(ftp.isConnected())
            {
                try
                { 
                    ftp.disconnect();

                }
                catch(Exception ioe)
                {

                }

            }

        }
        } 
}

当上传路径有一个文件夹时,它可以正常工作,例如。 /folder1/

但是当有子文件夹或多个目录时,它会上传字节0的空白文件,例如。 /folder1/folder2/

可能是什么问题?

【问题讨论】:

    标签: java file-upload ftp ftp-client


    【解决方案1】:

    ftp.storeFile(dirTree+zip_filename,input); 应该在for 创建所有子目录之后调用,并转到正确的目录。

    顺便说一句,可以帮助引入函数 makeAndGoToDirectory。

    【讨论】:

      【解决方案2】:

      Joop Eggen 明白了这一点,我想补充一点,如果您想检索文件/目录,编写以下代码是一个好习惯:

      String[] directories = dirTree.split(File.separator);

      而不是

      String[] directories = dirTree.split("/");

      因为它会使您的代码更便携。 FTP 服务器并不总是需要停留在 Unix/Linux 上。

      【讨论】:

        猜你喜欢
        • 2018-05-15
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多