Jump to content

Verificar a existencia de um ficheiro


Recommended Posts

Posted

Boas comunidade,

Necessitava de uma ajuda, se possível, que não estou a conseguir.

Quando faço um upload para a minha pasta de que se encontra no ftp server, antes de o fazer gostava de verificar se o nome  de um documento já existe. Para evitar o overwrite.

Desde já obrigado a todos

Posted (edited)

Já consegui... deixo aqui a solução para quem tiver a mesma duvida

FTPClient ftp = new FTPClient();
                    int port = 21;                  
                   ftp.connect("ftp server", port);
                   ftp.login("user","pass");                                      
                   ftp.enterLocalPassiveMode();          
                   ftp.setFileType(FTP.BINARY_FILE_TYPE);
                   
                   File fileName = save.getSelectedFile();
                   String localFile = "/" + fileName.getName();
                                                         
                   FileInputStream input = new FileInputStream(fileName);
                   System.out.println("total " + input.available());
                   
                   FTPFile files = ftp.mlistFile(localFile);
                   
                      if(files != null){
                         JOptionPane.showMessageDialog(null, "Já existe um documento com o mesmo nome."); 
                       }else{          
                           boolean out = ftp.storeFile(localFile, input);
                                input.close();
                            if(out){
                              System.out.println("Sucess");
                            }
                       }
Edited by Las Vegas

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.