文件上传到ftp

package cn.com.sparknet.fileStoreLiunx;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;

//LIUNX系统需要安装FTP服务
public class Ftp {
	
	
	
 
    //打印log日志
    private static final Log logger = LogFactory.getLog(Ftp.class);
 
    private static Date last_push_date = null;
 
    private Session sshSession;
 
    private ChannelSftp channel;
 
    private static ThreadLocal<Ftp> sftpLocal = new ThreadLocal<Ftp>();
    
 
    private Ftp(String host, int port, String username, String password){
    	try{
	        JSch jsch = new JSch();
	        jsch.getSession(username, host, port);
	        //根据用户名,密码,端口号获取session
	        sshSession = jsch.getSession(username, host, port);
	        sshSession.setPassword(password);
	        //修改服务器/etc/ssh/sshd_config 中 GSSAPIAuthentication的值yes为no,解决用户不能远程登录
	        sshSession.setConfig("userauth.gssapi-with-mic", "no");
	 
	        //为session对象设置properties,第一次访问服务器时不用输入yes
	        sshSession.setConfig("StrictHostKeyChecking", "no");
	        sshSession.connect();
	        //获取sftp通道
	        channel = (ChannelSftp)sshSession.openChannel("sftp");
	        channel.connect();
	        logger.info("连接ftp成功!" + sshSession);
    	}catch (Exception e) {
			// TODO: handle exception
		}
    }
 
    /**
     * 是否已连接
     *
     * @return
     */
    private boolean isConnected() {
        return null != channel && channel.isConnected();
    }
 
    
    /** 
     * 载单个文件 
     * @param directory       :远程下载目录(以路径符号结束) 如:/home/sftp/jiesuan_2/2014/
     * @param remoteFileName  FTP服务器文件名称 如:xxx.txt ||xxx.txt.zip 
     * @param localFile       本地文件路径 如 D:\\xxx.txt 
     * @return 
     * @throws GoPayException 
     */  
    public  void downloadFile(String directory, String remoteFileName,String localFile) throws Exception {   
        File file = null;  
        OutputStream output = null;  
        try {  
            file = new File(localFile);  
            if (file.exists()){  
                file.delete();  
            }  
            file.createNewFile();  
            channel.cd(directory);  
            output = new FileOutputStream(file);  
            channel.get(remoteFileName, output);  
        }  
        catch (SftpException e) {  
        	System.out.println("文件不存在或ftp目录或者文件异常,检查ftp目录和文件");
        }  
        catch (FileNotFoundException e) {  
        	System.out.println("本地目录异常,请检查");
        }  
        catch (IOException e) {  
        	System.out.println("创建本地文件失败"); 
        }  
        finally {  
            if (output != null) {  
                try {  
                    output.close();  
                }  
                catch (IOException e) {  
                	System.out.println("关闭流失败"); 
                }  
            }   
        }  
        System.out.println("ftp下载文件结束");
    }  
    
    /**
     * 获取本地线程存储的sftp客户端
     *
     * @return
     * @throws Exception
     */
    public static Ftp getSftpUtil(String host, int port, String username, String password) throws Exception {
        //获取本地线程
        Ftp sftpUtil = sftpLocal.get();
        if (null == sftpUtil || !sftpUtil.isConnected()) {
            //将新连接防止本地线程,实现并发处理
            sftpLocal.set(new Ftp(host, port, username, password));
        }
        return sftpLocal.get();
    }
 
    /**
     * 释放本地线程存储的sftp客户端
     */
    public static void release() {
        if (null != sftpLocal.get()) {
            sftpLocal.get().closeChannel();
            logger.info("关闭连接" + sftpLocal.get().sshSession);
            sftpLocal.set(null);
 
        }
    }
 
    /**
     * 关闭通道
     *
     * @throws Exception
     */
    public void closeChannel() {
        if (null != channel) {
            try {
                channel.disconnect();
            } catch (Exception e) {
                logger.error("关闭SFTP通道发生异常:", e);
            }
        }
        if (null != sshSession) {
            try {
                sshSession.disconnect();
            } catch (Exception e) {
                logger.error("SFTP关闭 session异常:", e);
            }
        }
    }
 
    /**
     * @param directory  上传ftp的目录
     * @param uploadFile 本地文件目录
     *
     */
    public void upload(String directory, File file) throws Exception {
    	createDir(directory,channel);
        InputStream input = new BufferedInputStream(new FileInputStream(file));
        channel.put(input, file.getName());
        if (input != null) {
        	input.close();
        }
        System.out.println("文件上传成功");
    }
    //获取文件
    public List<File> getFiles(String realpath, List<File> files) {
        File realFile = new File(realpath);
        if (realFile.isDirectory()) {
            File[] subfiles = realFile.listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    if (null == last_push_date ) {
                        return true;
                    } else {
                        long modifyDate = file.lastModified();
                        return modifyDate > last_push_date.getTime();
                    }
                }
            });
            for (File file : subfiles) {
                if (file.isDirectory()) {
                    getFiles(file.getAbsolutePath(), files);
                } else {
                    files.add(file);
                }
                if (null == last_push_date) {
                    last_push_date = new Date(file.lastModified());
                } else {
                    long modifyDate = file.lastModified();
                    if (modifyDate > last_push_date.getTime()) {
                        last_push_date = new Date(modifyDate);
                    }
                }
            }
        }
        return files;
    }
    
    
    /** 
     * 创建一个文件目录 
    * @throws Exception 
     */  
    public static void createDir(String createpath, ChannelSftp sftp)  {
    	 try {
			if (isDirExist(createpath, sftp)) {
				sftp.cd(createpath);
				return;
			}
			String pathArry[] = createpath.split("/");
			StringBuffer filePath = new StringBuffer("/");
			for (String path : pathArry) {
				if (path.equals("")) {
					continue;
				}
				filePath.append(path + "/");
				if (isDirExist(filePath.toString(), sftp)) {
					sftp.cd(filePath.toString());
				} else {
					// 建立目录
					sftp.mkdir(filePath.toString());
					// 进入并设置为当前目录
					sftp.cd(filePath.toString());
				}
			}
			sftp.cd(createpath);
		  } catch (Exception e) {
			  System.out.println("创建路径错误:");
		  }
       }  

    /** 
     * 判断目录是否存在 
     */  
    public static boolean isDirExist(String directory,ChannelSftp sftp) {  
       boolean isDirExistFlag = false;
       try {
           SftpATTRS sftpATTRS = sftp.lstat(directory);
           isDirExistFlag = true;
           return sftpATTRS.isDir();
       } catch (Exception e) {
           if (e.getMessage().toLowerCase().equals("no such file")) {
               isDirExistFlag = false;
           }
       }
       return isDirExistFlag;
    } 
    
    public static void main(String[] args) {
    	try {
			Ftp ftp=getSftpUtil("192.168.1.137", 22, "root", "root123.");
			File file = new File("D:/登记模板.doc");
			ftp.upload("/home/jihu",file);
			
			ftp.downloadFile("/home/jihu/","登记模板.doc","E:/登记模板2.doc");
			release();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spark 读取 linux sftp上的文本文件,原jar只支持josn,csv等,增加bcp,txt文件的支持 下面是例子: public static void main(String[] args) throws Exception { SparkConf conf = new SparkConf().setMaster("local").setAppName("SparkDataFrame"); JavaSparkContext javacontext = new JavaSparkContext(conf); SQLContext sqlContext = new SQLContext(javacontext); Dataset<Row> df = sqlContext.read(). format("com.springml.spark.sftp"). option("host", "192.168.1.3"). option("username", "root"). option("password", "111111"). option("fileType", "bcp"). load("/sparktest/sparkfile0.bcp"); /*List<Row> list = df.collectAsList(); for(Row row:list){ String[] words = new String(row.getString(0).getBytes(),0,row.getString(0).length(),"UTF-8").split(" ",-1); for(int i=0;i<words.length;i++){ System.out.println("words==="+words[i]); } }*/ JavaRDD<Row> rowRdd = df.javaRDD(); JavaRDD<Row> words_bcp= rowRdd.map(new Function<Row, Row>() { @Override public Row call(Row row) throws Exception { // TODO Auto-generated method stub String line = row.getString(0); String[] words = new String(line.getBytes(),0,line.getBytes().length,"utf-8").split(" ",-1); return RowFactory.create(words); } }); List<Row> list = words_bcp.collect(); for(Row row:list){ System.out.println("row1=="+row.getString(0)); } df.write().format("com.springml.spark.sftp"). option("host", "192.168.1.3"). option("username", "root"). option("password", "111111"). option("fileType", "bcp"). save("/sparktest/luozhao.bcp"); df.show(); javacontext.close(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值