---
title: "SFTP Java Client -- jcraft.jsch "
author: "Jameslyy"
date: "2017-04-24T11:17:55Z"
tags: ["Java", "jcraft", "jsch", "sftp"]
categories: ["编程语言"]
slug: "sftp-java-client-jcraft-jsch"
draft: false
lastmod: "2017-04-24T11:19:01Z"
id: 2370959
url: "https://jamesblog.iteye.com/blog/2370959"
---

[http://www.jcraft.com/jsch](http://www.jcraft.com/jsch/)

 

```
        JSch jsch = new JSch();
        ChannelSftp channelSftp = null;
        Session session = null;
        try{
            session = jsch.getSession(userName, host, port);
            session.setPassword(password);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();

            channelSftp = (ChannelSftp)session.openChannel("sftp");
            channelSftp.connect();

            channelSftp.cd(dirTransactions);
            OutputStream outputStream = channelSftp.put(fileName);
            outputStream.write(content.getBytes());

            outputStream.flush();
            outputStream.close();
        } catch (JSchException e) {
            e.printStack();
        } catch (SftpException e) {
            e.printStack();
        } catch (IOException e) {
            e.printStack();
        } finally{
            if (channelSftp != null)
                channelSftp.quit();
            if (session != null)
                session.disconnect();
        }
    
```

 

```
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        channelSftp.get(dir + fileName, outputStream);
        content = new String(outputStream.toByteArray());
```