How to check whether the specified file exists in ftp server in java
During the project work, it is necessary to check whether the specified file in the ftp server exists. I checked the relevant information on the Internet and can implement it through the ftpClient class.
Free learning video tutorial sharing: java course
Specific implementation code:
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; /** * 检验指定路径的文件是否存在ftp服务器中 * @param filePath--指定绝对路径的文件 * @param user--ftp服务器登陆用户名 * @param passward--ftp服务器登陆密码 * @param ip--ftp的IP地址 * @param port--ftp的端口号 * @return */ public static boolean isFTPFileExist(String filePath,String user,String passward,String ip,int port){ FTPClient ftp = new FTPClient(); try { // 连接ftp服务器 ftp.connect(ip, port); // 登陆 ftp.login(user, passward); // 检验登陆操作的返回码是否正确 if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){ ftp.disconnect(); return false; } ftp.enterLocalActiveMode(); // 设置文件类型为二进制,与ASCII有区别 ftp.setFileType(FTP.BINARY_FILE_TYPE); // 设置编码格式 ftp.setControlEncoding("GBK"); // 提取绝对地址的目录以及文件名 filePath = filePath.replace("ftp://"+ip+":"+port+"/", ""); String dir = filePath.substring(0, filePath.lastIndexOf("/")); String file = filePath.substring(filePath.lastIndexOf("/")+1); // 进入文件所在目录,注意编码格式,以能够正确识别中文目录 ftp.changeWorkingDirectory(new String(dir.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING)); // 检验文件是否存在 InputStream is = ftp.retrieveFileStream(new String(file.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING)); if(is == null || ftp.getReplyCode() == FTPReply.FILE_UNAVAILABLE){ return false; } if(is != null){ is.close(); ftp.completePendingCommand(); } return true; } catch (Exception e) { e.printStackTrace(); }finally{ if(ftp != null){ try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return false; }
Recommended related article tutorials: java introductory tutorial
The above is the detailed content of How to check whether the specified file exists in ftp server in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

AdeadlockinJavaoccurswhentwoormorethreadsareblockedforever,eachwaitingforaresourceheldbytheother,typicallyduetocircularwaitcausedbyinconsistentlockordering;thiscanbepreventedbybreakingoneofthefournecessaryconditions—mutualexclusion,holdandwait,nopree
![You are not currently using a display attached to an NVIDIA GPU [Fixed]](https://img.php.cn/upload/article/001/431/639/175553352135306.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Ifyousee"YouarenotusingadisplayattachedtoanNVIDIAGPU,"ensureyourmonitorisconnectedtotheNVIDIAGPUport,configuredisplaysettingsinNVIDIAControlPanel,updatedriversusingDDUandcleaninstall,andsettheprimaryGPUtodiscreteinBIOS/UEFI.Restartaftereach

UseOptional.empty(),Optional.of(),andOptional.ofNullable()tocreateOptionalinstancesdependingonwhetherthevalueisabsent,non-null,orpossiblynull.2.CheckforvaluessafelyusingisPresent()orpreferablyifPresent()toavoiddirectnullchecks.3.Providedefaultswithor

TheOilPaintfilterinPhotoshopisgreyedoutusuallybecauseofincompatibledocumentmodeorlayertype;ensureyou'reusingPhotoshopCS6orlaterinthefulldesktopversion,confirmtheimageisin8-bitperchannelandRGBcolormodebycheckingImage>Mode,andmakesureapixel-basedlay

Understand JCA core components such as MessageDigest, Cipher, KeyGenerator, SecureRandom, Signature, KeyStore, etc., which implement algorithms through the provider mechanism; 2. Use strong algorithms and parameters such as SHA-256/SHA-512, AES (256-bit key, GCM mode), RSA (2048-bit or above) and SecureRandom; 3. Avoid hard-coded keys, use KeyStore to manage keys, and generate keys through securely derived passwords such as PBKDF2; 4. Disable ECB mode, adopt authentication encryption modes such as GCM, use unique random IVs for each encryption, and clear sensitive ones in time

Micronautisidealforbuildingcloud-nativeJavaapplicationsduetoitslowmemoryfootprint,faststartuptimes,andcompile-timedependencyinjection,makingitsuperiortotraditionalframeworkslikeSpringBootformicroservices,containers,andserverlessenvironments.1.Microna

The core of SpringDataJPA and Hibernate working together is: 1. JPA is the specification and Hibernate is the implementation, SpringDataJPA encapsulation simplifies DAO development; 2. Entity classes map database structures through @Entity, @Id, @Column, etc.; 3. Repository interface inherits JpaRepository to automatically implement CRUD and named query methods; 4. Complex queries use @Query annotation to support JPQL or native SQL; 5. In SpringBoot, integration is completed by adding starter dependencies and configuring data sources and JPA attributes; 6. Transactions are made by @Transactiona

RuntheapplicationorcommandasAdministratorbyright-clickingandselecting"Runasadministrator"toensureelevatedprivilegesaregranted.2.CheckUserAccountControl(UAC)settingsbysearchingforUACintheStartmenuandsettingtheslidertothedefaultlevel(secondfr
