ftp上传文件

package com.lck.util.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

public class TestFtp
{
    public static void main(String[] args)
    {
        try
        {
            FTPClient ftp = new FTPClient();
            ftp.connect("192.168.67.242", 21);
            boolean result = ftp.login("jiangsu_test", "jiangsu_test");
            System.out.println("login:"+result);
            File eclipse = new File("E:\\study\\JS_note.pdf");
            System.out.println(eclipse.length());
            FileInputStream fis = new FileInputStream(eclipse);
            ftp.setControlEncoding("GBK");
            ftp.enterLocalPassiveMode();
            ftp.changeWorkingDirectory("/home/jiangsu_test/test");
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            result = ftp.storeFile("~JS_note.pdf", fis);
            System.out.println(result);
            if(result)
            {
                System.out.println("文件上传成功!");
                result = ftp.rename("~JS_note.pdf", "JS_note.pdf");
                if(result)
                {
                    System.out.println("改文件名成功!");
                }
            }
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

  引入commons-net-3.3.jar

你可能感兴趣的:(ftp)