标签:des style java 使用 os io 数据 for
简述
>> FTP:
1. Install FTP service on Linux(Red Hat) as root user【FTP Upload】
package shuai.study.ftp; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; /** * @Description: FTP Upload * @author Zhou Shengshuai * @date 2014年8月5日 下午2:37:57 * */ public class FtpUpload { private static final Logger logger = LogManager.getLogger(FtpUpload.class); static { System.setProperty("FTP_LOG", "/home/tmp/log"); DOMConfigurator.configure("/home/tmp/configuration/log4j.xml"); } private static String hostname = "192.168.0.1"; private static int ftpPort = 21; private static String ftpUsername = "ftpuser"; private static String ftpPassword = "ftpPassword"; private static String remoteDirectoryString = "/upload"; private static File localDirectory = new File("/home/tmp/local"); public static Set<File> upload(Collection<File> localFileCollection) { Set<File> localFileSet = new HashSet<File>(); FTPClient ftpClient = connect(hostname, ftpPort, ftpUsername, ftpPassword, remoteDirectoryString); if (ftpClient != null && ftpClient.isConnected()) { Iterator<File> localFileIterator = localFileCollection.iterator(); while (localFileIterator.hasNext()) { File localFile = localFileIterator.next(); if (upload(ftpClient, localFile)) { localFileSet.add(localFile); } } } disconnect(ftpClient); return localFileSet; } private static boolean upload(FTPClient ftpClient, File localFile) { boolean storeFileFlag = false; if (localFile != null && localFile.isFile()) { InputStream fileInputStream = null; try { fileInputStream = new FileInputStream(localFile); storeFileFlag = ftpClient.storeFile(localFile.getName(), fileInputStream); if (storeFileFlag) { logger.info("Upload local file " + localFile + " to remote directory " + ftpUsername + "@" + hostname + ":" + remoteDirectoryString + " via FTP"); } else { logger.error("Fail to upload local file " + localFile + " to remote directory " + ftpUsername + "@" + hostname + ":" + remoteDirectoryString + " via FTP"); } } catch (IOException ioe) { logger.error("FTP Upload Exception", ioe); } finally { IOUtils.closeQuietly(fileInputStream); } } return storeFileFlag; } private static FTPClient connect(String hostname, int ftpPort, String ftpUsername, String ftpPassword, String remoteDirectoryString) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(hostname, ftpPort); logger.info("FTP Connected " + hostname + ":" + ftpPort); ftpClient.login(ftpUsername, ftpPassword); logger.info("FTP Logined as " + ftpUsername); // ftpClient.enterLocalPassiveMode(); // if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { // logger.error("FTP Response Unsuccessfully"); // disconnect(ftpClient); // } if (ftpClient.makeDirectory(remoteDirectoryString)) { logger.info("FTP Directory Made " + remoteDirectoryString); } ftpClient.changeWorkingDirectory(remoteDirectoryString); logger.info("FTP Working Directory Changed as " + remoteDirectoryString); // ftpClient.setBufferSize(1024); // ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); } catch (IOException ioe) { logger.error("FTP Connection Exception", ioe); } return ftpClient; } private static void disconnect(FTPClient ftpClient) { if (ftpClient != null) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ioe) { logger.error("FTP Disconnection Exception", ioe); } } logger.info("FTP Disconnected"); } private static void delete(Set<File> exportCucFileSet) { Iterator<File> exportCucFileIterator = exportCucFileSet.iterator(); while (exportCucFileIterator.hasNext()) { File exportCucFile = exportCucFileIterator.next(); if (FileUtils.deleteQuietly(exportCucFile)) { logger.info("Delete local file " + exportCucFile + " after FTP upload"); } else { logger.error("Fail to delete local file " + exportCucFile + " after FTP upload"); } } } public static void main(String[] args) { Collection<File> exportCucFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true); Set<File> exportCucFileSet = FtpUpload.upload(exportCucFileCollection); delete(exportCucFileSet); } }
package shuai.study.sftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Collection; import java.util.Iterator; import java.util.Properties; import org.apache.commons.io.FileUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; /** * @Description: SFTP Upload * @author Zhou Shengshuai * @date 2014年8月1日 下午2:37:57 * */ public class SftpUpload { private static final Logger logger = LogManager.getLogger(SftpUpload.class); static { System.setProperty("FTP_LOG", "/home/tmp/log"); DOMConfigurator.configure("/home/tmp/configuration/log4j.xml"); } private static String hostname = "192.168.0.1"; private static int sftpPort = 22; private static String sftpUsername = "ftpuser"; private static String sftpPassword = "ftpPassword"; private static String remoteDirectoryString = "/upload"; private static String localDirectoryString = "/home/tmp/local"; /** * @Description: Upload */ public static void upload() { File localDirectory = new File(localDirectoryString); if (localDirectory.exists()) { ChannelSftp sftp = connect(hostname, sftpPort, sftpUsername, sftpPassword); Collection<File> localFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true); Iterator<File> localFileIterator = localFileCollection.iterator(); while (localFileIterator.hasNext()) { File localFile = localFileIterator.next(); logger.info("Local File: " + localFile); if (sftp != null && sftp.isConnected()) { upload(remoteDirectoryString, localFile, sftp); } } disconnect(sftp); } else { logger.warn("The Local Directory " + localDirectoryString + " doesn't exist"); } } /** * @Description: SFTP Upload * * @param remoteDirectoryString * Remote CM Directory * @param localFile * Local CM File * @param sftp * ChannelSftp */ public static void upload(String remoteDirectoryString, File localFile, ChannelSftp sftp) { logger.info("Remote Directory: " + remoteDirectoryString); try { sftp.cd(remoteDirectoryString); sftp.put(new FileInputStream(localFile), localFile.getName()); FileUtils.deleteQuietly(localFile); logger.info("Upload Local File " + localFile + " via SFTP"); } catch (FileNotFoundException fnfe) { logger.error("File Not Found Exception", fnfe); } catch (SftpException se) { logger.error("SFTP Upload Exception", se); } } /** * @Description: SFTP Connection * * @param hostname * SFTP HOST * @param sftpPort * SFTP PORT * @param sftpUsername * SFTP USERNAME * @param sftpPassword * SFTP PASSWORD * * @return ChannelSftp */ public static ChannelSftp connect(String hostname, int sftpPort, String sftpUsername, String sftpPassword) { JSch jsch = new JSch(); Properties sshConfig = new Properties(); Channel channel = null; try { Session session = jsch.getSession(sftpUsername, hostname, sftpPort); logger.info("Session Created"); session.setPassword(sftpPassword); sshConfig.put("StrictHostKeyChecking", "no"); session.setConfig(sshConfig); // session.setTimeout(60000); // session.setServerAliveInterval(90000); session.connect(); logger.info("Session Connected"); channel = session.openChannel("sftp"); logger.info("Channel Opened"); channel.connect(); logger.info("Channel Connected"); } catch (JSchException je) { logger.error("SFTP Exception", je); } return (ChannelSftp) channel; } /** * @Description: Disconnect SFTP * * @param sftp * ChannelSftp */ public static void disconnect(ChannelSftp sftp) { if (sftp != null) { try { sftp.getSession().disconnect(); } catch (JSchException je) { logger.error("SFTP Disconnect Exception", je); } sftp.disconnect(); } } /** * @Description: Main Thread * @param args */ public static void main(String[] args) { SftpUpload.upload(); } }
SFTP & FTP Upload,布布扣,bubuko.com
标签:des style java 使用 os io 数据 for
原文地址:http://blog.csdn.net/three_man/article/details/38418947