import java.io.*; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; import java.util.Vector; import org.apache.commons.io.IOUtils; import com.jcraft.jsch.*; import lombok.extern.slf4j.Slf4j; @Slf4j public class SftpUtil { private final String ftpAddress; private final int ftpPort; private final String ftpUserName; private final String ftpPassWord; private Session session; private ChannelSftp sftp; public SftpUtil(String odsRead, String ftpAddress, int ftpPort, String ftpUserName, String ftpPassWord, String ftpFile) { this.ftpAddress = ftpAddress; this.ftpPort = ftpPort; this.ftpUserName = ftpUserName; this.ftpPassWord = ftpPassWord; } /** * 连接sftp服务器 */ public boolean sftpLogin(){ try { JSch jsch = new JSch(); session = jsch.getSession(ftpUserName,ftpAddress,ftpPort); session.setPassword(ftpPassWord); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; return true; } catch (JSchException e) { log.info("登录失败:地址:{},端口:{},用户名:{},密码:{}",ftpAddress,ftpPort,ftpUserName,ftpPassWord); } return false; } /** * 关闭连接 server */ public void logout(){ if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } } if (session != null) { if (session.isConnected()) { session.disconnect(); } } } /** * 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory * @param basePath 服务器的基础路径 * @param directory 上传到该目录 * @param sftpFileName sftp端文件名 */ public void upload(String basePath,String directory, String sftpFileName, InputStream input) throws SftpException{ try { sftp.cd(basePath); sftp.cd(directory); } catch (SftpException e) { //目录不存在,则创建文件夹 String [] dirs=directory.split("/"); String tempPath=basePath; for(String dir:dirs){ if(null== dir || "".equals(dir)) { continue; } tempPath+="/"+dir; try{ sftp.cd(tempPath); }catch(SftpException ex){ sftp.mkdir(tempPath); sftp.cd(tempPath); } } } sftp.put(input, sftpFileName); } /** * 下载文件。 * @param directory 下载目录 * @param downloadFile 下载的文件 * @param saveFile 存在本地的路径 */ public boolean download(String directory, String downloadFile, String saveFile){ try { if (directory != null && !"".equals(directory)) { sftp.cd(directory); } File file = new File(saveFile); FileOutputStream fileOutputStream = new FileOutputStream(file); sftp.get(downloadFile,fileOutputStream); fileOutputStream.close(); return true; } catch (SftpException | IOException e) { e.printStackTrace(); } return false; } public boolean checkFileWritingOn(String fileName) throws Exception{ long oldLen = 0; long newLen = 0; File file = new File(fileName); while(true){ newLen = file.length(); if ((newLen - oldLen) > 0) { oldLen = newLen; System.out.println(file.length()); Thread.sleep(10000); } else { return true; } } } /** * 等待文件(非目录)读写完毕,费时的操作,不要放在主线程 * * @param file 文件 */ public void waitForWirtenCompleted(File file) { if (!file.exists()) { return; } long oldLength; do { oldLength = file.length(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } while (oldLength != file.length()); } /** * 下载文件 * @param directory 下载目录 * @param downloadFile 下载的文件名 * @return 字节数组 */ public byte[] download(String directory, String downloadFile) throws SftpException, IOException{ if (directory != null && !"".equals(directory)) { sftp.cd(directory); } InputStream is = sftp.get(downloadFile); return IOUtils.toByteArray(is); } /** * 删除文件 * @param directory 要删除文件所在目录 * @param deleteFile 要删除的文件 */ public void delete(String directory, String deleteFile) throws SftpException{ sftp.cd(directory); sftp.rm(deleteFile); } /** * 列出目录下的文件 * @param directory 要列出的目录 */ public Vector<?> listFiles(String directory) throws SftpException { return sftp.ls(directory); } }
STPF取数协议
最新推荐文章于 2024-05-12 15:57:04 发布