if (StringUtils.isBlank(bdcdyh)) { throw new ServiceRuntimeException("bdcdyh is null"); } String fileName = signApprovedMapper.selectDzht(bdcdyh); FTPClient ftpClient = new FTPClient(); String re = null; InputStream inputStream = null; try { ftpClient.connect(ftpHost, ftpPort);//ip地址,端口号 ftpClient.login(user, password);//账户,密码 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //是否成功登录服务器 int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); } inputStream = ftpClient.retrieveFileStream(fileName); if (inputStream != null) { byte[] data = null; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); data = new byte[inputStream.available()]; int len = 0; while ((len = inputStream.read(data)) != -1) { outStream.write(data, 0, len); } data = outStream.toByteArray(); Base64.Encoder encoder = Base64.getEncoder(); re = encoder.encodeToString(data); } } catch (Exception e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return re;
查找ftp上的文件,并将文件转换为base64文件流返回
最新推荐文章于 2024-08-09 13:53:06 发布