package cn.mcangpartner.test;


import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * Created by qiu on 2019/4/10.
 */
public class FtpTest {

    public static void main(String[] args) {
        FtpClient client = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            String url = "192.168.101.110";
            int port = 21;
            SocketAddress addr = new InetSocketAddress(url, port);
            client = FtpClient.create();
            client.connect(addr);
            //匿名ftp默认用户ftp密码随便填
            client.login("ftp", "00544".toCharArray());
            client.setBinaryType();
            // 将ftp文件加入输出流中。输出到ftp上
            os = client.putFileStream("1.xls");
            File file = new File("E:\\excel\\1.xls");
            // 创建一个缓冲区
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = fis.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("上传完成!");
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}