1

使用下面的程序,我可以在 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)
                    {

                    }

                }

            }
            } 
    }
4

1 回答 1

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...

}
于 2012-07-20T06:37:16.080 回答