POM 添加依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.7</version> </dependency> <!--添加依赖--> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency>
Code
Ftp ftp = new Ftp("ftpIp", ftpPort, "ftpUser", "ftpPassword"); try { //匿名登录,一定要设 被动模式 ftp.setMode(FtpMode.Passive); //进入远程目录 //ftp.cd("/opt/upload"); File reportFile = FileUtil.file("D:/report.pdf"); boolean flag = ftp.upload("/opt/upload", reportFile); if (!flag) { throw new CustomException(7002, "FTP 上传报告失败"); } //下载远程文件 //ftp.download("/opt/upload", "test.jpg", FileUtil.file("e:/test2.jpg")); return StrUtil.format("ftp://{}:{}{}{}", hisFtpIp, hisFtpPort, ftpRemotePath, pdfFileName); } catch (Exception ex) { toHisLogger.error(ex.getMessage(), ex); throw ex; } finally { ftp.close(); }