Memo: [Java] Get Raspberry Pi data by SFTP

background

The workplace collects data on the atmosphere and ultraviolet rays with a Raspberry Pi. Initially I copied it locally with WinSCP to borrow that data, but after doing so many times I was so tired that I thought about getting the data via SFTP. I will write down the code.

ラズパイのデータコピーしてたんですけども.png

** SSH ** ... ** Encrypted communication ** ** FTP ** ... ** File transfer protocol **

** SSH + FTP = SFTP Transfer files using SSH-encrypted communication **

** Output Raspberry Pi data as standard **

Currently, data is stored in Raspberry Pi like this. This time I will output the data of the file 20180716.csv. 1.png -** Upload / download with Java SFTP (no known_hosts required) ** -** File operation by sftp with Java + JSch ** : grinning: I referred to this page. Thank you very much.

Add jsch's ** <dependency> ** tag inside the ** <dependencies> ** tag of pom.xml. Like this

pom.xml


    <dependencies>
		<!--JSch_adding_at_self_-->
		<dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
    </dependencies>

The code is almost a copy of the reference page.

test.java


import com.jcraft.jsch.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class test {

    public static void main(String[] args) throws JSchException, SftpException, FileNotFoundException, IOException {

        String host = "192.168.29.29"; 
        int port = 22;
        String user = "pi";
        String password = "raspberry";
        String dir = "/home/pi/iot/pressure/data/2018/";
        String fileName = "20180716.csv";

        JSch jsch;
        Session session = null;
        ChannelSftp channel = null;
        FileInputStream fin = null;
        BufferedInputStream bin = null;

        try {
            //Connect
            jsch = new JSch();
            session = jsch.getSession(user, host, port);
            session.setConfig("StrictHostKeyChecking", "no");//known_Skip hosts check
            session.setPassword(password);
            session.connect();

            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            channel.cd(dir);
          
            //download
            bin = new BufferedInputStream(channel.get(fileName));
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int length;
            while (true) {
                length = bin.read(buf);
                if (length == -1) {
                    break;
                }
                bout.write(buf, 0, length);
            }
            //Standard output
            System.out.format("%1$s", new String(bout.toByteArray(), StandardCharsets.UTF_8));
        }
        finally {
            if (fin != null) {
                try {
                    fin.close();
                }
                catch (IOException e) {
                }
            }
            if (bin != null) {
                try {
                    bin.close();
                }
                catch (IOException e) {
                }
            }
            if (channel != null) {
                try {
                    channel.disconnect();
                }
                catch (Exception e) {
                }
            }
            if (session != null) {
                try {
                    session.disconnect();
                }
                catch (Exception e) {
                }
            }
        }
    }
}

Let's run it. The contents of CSV have been exported. 2.png

** Output Raspberry Pi file locally **

Now let's output the data to a local file. I just wrote the standard output of the above code to a file and changed it to the following contents.

test.java


import com.jcraft.jsch.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class test {

    public static void main(String[] args) throws JSchException, SftpException, FileNotFoundException, IOException {

        String host = "192.168.29.29";
        int port = 22;
        String user = "pi";
        String password = "raspberry";
        String dir = "/home/pi/iot/pressure/data/2018/";
        String fileName = "20180716.csv";

        JSch jsch;
        Session session = null;
        ChannelSftp channel = null;
        FileInputStream fin = null;
        BufferedInputStream bin = null;

        try {
            
            jsch = new JSch();
            session = jsch.getSession(user, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            channel.cd(dir);

            bin = new BufferedInputStream(channel.get(fileName));
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int length;
            while (true) {
                length = bin.read(buf);
                if (length == -1) {
                    break;
                }
                bout.write(buf, 0, length);
            }
            File file = new File("C:/tedkuma/BOX/★"+fileName);//Export to file
            file.createNewFile();
            FileWriter fw = new FileWriter(file);
            fw.write(new String(bout.toByteArray()));
            fw.close();
            System.out.print("Output successful");
        }
        finally {
            if (fin != null) {
                try {
                    fin.close();
                }
                catch (IOException e) {
                }
            }
            if (bin != null) {
                try {
                    bin.close();
                }
                catch (IOException e) {
                }
            }
            if (channel != null) {
                try {
                    channel.disconnect();
                }
                catch (Exception e) {
                }
            }
            if (session != null) {
                try {
                    session.disconnect();
                }
                catch (Exception e) {
                }
            }
        }
    }
}

The output is displayed successfully on the console, and a file with a star is created in the target directory. 3.png

The contents seem to be okay. The same data as before is output in CSV format. 4.png

** Output files within 1 day of update date locally **

Diverting the content of the article I wrote the other day Identify files that have been updated within 1 day I will try to output only the files within 1 day of the update date locally.

The ChannelSftp.LsEntry page will be helpful. You can receive the file attributes from LsEntry with getAttrs () and get the modification date with getMTime ().

I personally stumbled upon the information in the filelist when I received the information on the Raspberry Pi side. I received an error (. or ..) in addition to the file. Since the directory can be determined by isDir (), it is processed if it is not a directory. Rlogin.png

test.java


import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

public class test {

    public static void main(String[] args) throws JSchException, SftpException, FileNotFoundException, IOException {

        String host = "192.168.29.29";
        int port = 22;
        String user = "pi";
        String password = "raspberry";
        String dir = "/home/pi/iot/pressure/data/2018/";

        JSch jsch;
        Session session = null;
        ChannelSftp channel = null;
        FileInputStream fin = null;
        BufferedInputStream bin = null;

        try {
            
            jsch = new JSch();
            session = jsch.getSession(user, host, port);
            session.setConfig("StrictHostKeyChecking", "no");   
            session.setPassword(password);
            session.connect();

            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            channel.cd(dir);

            Calendar st = Calendar.getInstance();   //Get the current date and time with the Calendar class
            st.add(Calendar.DATE, -1);              //Get 1 day ago
            Date start = st.getTime();              //Change to Date

            Calendar en = Calendar.getInstance();
            en.add(Calendar.MINUTE, -10);           //Get 10 minutes ago
            Date end = en.getTime();

            Vector<?> filelist = channel.ls(dir);
            for(int i=0; i<filelist.size();i++){

                LsEntry entry = (LsEntry) filelist.get(i);
                String filename= entry.getFilename();
                SftpATTRS attrs =  entry.getAttrs();
                Date lastModified = new Date(attrs.getMTime() * 1000L);
                if(attrs.isDir()) {
                    //If it's a directory, do nothing
                }if(start.compareTo(lastModified)<0 && end.compareTo(lastModified)>0){
                    bin = new BufferedInputStream(channel.get(filename)); 
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    byte[] buf = new byte[1024];
                    int length;
                    while (true) {
                        length = bin.read(buf);
                        if (length == -1) {
                            break;
                        }
                        bout.write(buf, 0, length);
                    }
                    File file = new File("C:/tedkuma/★"+filename);
                    file.createNewFile();
                    FileWriter fw = new FileWriter(file);
                    fw.write(new String(bout.toByteArray()));
                    fw.close();
                    System.out.print("Output successful");
                }
            }
        }
        finally {
            if (fin != null) {
                try {
                    fin.close();
                }
                catch (IOException e) {
                }
            }
            if (bin != null) {
                try {
                    bin.close();
                }
                catch (IOException e) {
                }
            }
            if (channel != null) {
                try {
                    channel.disconnect();
                }
                catch (Exception e) {
                }
            }
            if (session != null) {
                try {
                    session.disconnect();
                }
                catch (Exception e) {
                }
            }
        }
    }
}

Only files within 1 day of modification are copied to the target directory with names marked with ★. 5.png

Since the data from last night is included, the contents of the data do not seem to be a problem. 6.png

Recommended Posts

Memo: [Java] Get Raspberry Pi data by SFTP
Java learning memo (data type)
Process the motion detected by the motion sensor HC-SR501 with Raspberry Pi 3 & Java
[Personal memo] Java data type is annoying
Java memo
Organized memo in the head (Java --Data type)
Get data with api created by curl command
java anything memo
Java Silver memo
java, maven memo
Java SE 7 memo
java anything memo 2
Pi in Java
Java specification memo
Java pattern memo
Display characters on I2C 1602 LCD with Raspberry Pi 3 & Java
About CLDR locale data enabled by default from Java 9
Read temperature / humidity with Java from Raspberry Pi 3 & DHT11