文件复制到未在java中移动的FTP服务器上

时间:2012-07-20 06:26:27

标签: java ftp ftp-client

使用以下程序我可以在ftp服务器上传zip文件。 但它制作了zip文件的副本并将文件上传到ftp服务器上。 我希望它应该从本地系统删除该文件并将其复制到服务器,即它应该移动文件而不是复制。请指导

public class UploadFile {
        public static void main(String args[])
        {      
            FTPClient ftp=new FTPClient();
            try {          
                int reply;  

                ftp.connect("ipadddress"); 

                ftp.login("abc", "abc"); 

                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("D:\\testencrypted.zip");
                String dirTree="/Vel";
                boolean dirExists = true;
                String[] directories = dirTree.split("/");
                for (String dir : directories )
                {
                    if (!dir.isEmpty() )
                    {
                        if (dirExists)
                        {
                            dirExists = ftp.changeWorkingDirectory(dir);
                        }
                        else 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+"/t1.zip",input);
                input.close();
                ftp.logout();        
                } 
            catch(Exception e) 
                {                      
                System.out.println("err"+ e);             
                }
            finally 
            {
                if(ftp.isConnected())
                {
                    try
                    { 
                        ftp.disconnect();

                    }
                    catch(Exception ioe)
                    {

                    }

                }

            }
            } 
    }

1 个答案:

答案 0 :(得分:1)

因此,一旦您完成上传(并且您确定它已成功完成,只需使用File.delete()从本地磁盘中删除该文件。

File sourceFile = new File("D:\\testencrypted.zip");    
InputStream input= new FileInputStream(sourceFile);

// Upload the file...
// Make sure you close the input stream first ;)

if (!sourceFile.delete()) {

  System.out.println("Failed to delete " + sourceFile + " from local disk");
  sourceFile.deleteOnExit(); // try and delete on JVM exit...

}