文件被复制在FTP服务器中不移动在java

问题描述:

使用下面的程序,我可以上传zip文件在FTP服务器上。 但它使zip文件的副本上传到ftp服务器上。 我想它应该删除本地系统中的文件,并将其复制到服务器即它应该文件被复制在FTP服务器中不移动在java

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) 
        { 

        } 

       } 

      } 
      } 
    } 

所以不移动copy.Please文件指导,一旦你完成了上传(和你确定它是成功的,只需使用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... 

}