一开始百度搜了,用的 FTPClient ftpClient = new FTPClient(); 不是530就是连接异常,我以为是FTP设置的原因,看了用户ftp关于用户黑白名单也没错,端口号与IP也能ping通
后来从从别的项目里面复制的代码 ftpName(ftp用户名),ftpConnect(ftpip地址),ftpPassWord(ftp密码),ftpfile(ftp文件路径),就当保存下次也许用得上
public static void uploadFile( String fileName,InputStream input) { JSch jsch = new JSch(); Session session = null; ChannelSftp sftp = null; Channel channel = null; try{ session = jsch.getSession(ftpName, ftpConnect, 22); session.setPassword(ftpPassWord); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; logger.info("开始上传"); sftp.put(input, ftpfile+ fileName , ChannelSftp.OVERWRITE); logger.info("上传成功"); }catch (Exception e){ e.printStackTrace(); } finally { if (session != null) { if (session.isConnected()) { session.disconnect(); } } if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } } if (channel != null) { if (channel.isConnected()) { channel.disconnect(); } } } }