Java,通过自动扫描本地文件夹下所有的TXT和zip文件,然后通过FTP上传到目标地址下

Java,通过自动扫描本地文件夹下所有的TXT和zip文件,然后通过FTP上传到目标地址下,两个地址都是动态的,不是写死的。需要源码和相关依赖配置

方法1

    import java.io.File;
    import java.io.FileInputStream;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPReply;

    public class test {

        private  FTPClient ftp;
        /**
         *
         * @param path 上传到ftp服务器哪个路径下
         * @param addr 地址
         * @param port 端口号
         * @param username 用户名
         * @param password 密码
         * @return
         * @throws Exception
         */
        private  boolean connect(String path,String addr,int port,String username,String password) throws Exception {
            boolean result = false;
            ftp = new FTPClient();
            int reply;
            ftp.connect(addr,port);
            ftp.login(username,password);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(path);
            result = true;
            return result;
        }
        /**
         *
         * @param file 上传的文件或文件夹
         * @throws Exception
         */
        private void upload(File file) throws Exception{
            if(file.isDirectory()){
                ftp.makeDirectory(file.getName());
                ftp.changeWorkingDirectory(file.getName());
                String[] files = file.list();
                for (int i = 0; i < files.length; i++) {
                    File file1 = new File(file.getPath()+"\\"+files[i] );
                    if(file1.isDirectory()){
                        upload(file1);
                        ftp.changeToParentDirectory();
                    }else{
                        File file2 = new File(file.getPath()+"\\"+files[i]);
                        FileInputStream input = new FileInputStream(file2);
                        ftp.storeFile(file2.getName(), input);
                        input.close();
                    }
                }
            }else{
                File file2 = new File(file.getPath());
                FileInputStream input = new FileInputStream(file2);
                ftp.storeFile(file2.getName(), input);
                input.close();
            }
        }
       public static void main(String[] args) throws Exception{
          test t = new test();
          t.connect("", "localhost", 21, "yhh", "yhhazr");
          File file = new File("e:\\uploadify");
          t.upload(file);
       }
    }

    https://www.cnblogs.com/tianhyapply/p/3721370.html

    用这个代码
    ff(file1.isDirectory()){ //这里可以通过文件名对文件类型判断,比如不是txt zip不执行下面的

方法2

https://zhidao.baidu.com/question/1644130610953513500.html

@baiecho off
echo open 192.168.137.1 21 >> tempconfig.txt
echo user lzqftp lzq123>> tempconfig.txt
echo bin >> tempconfig.txt
for /f "delims=? tokens=* eol=?" %%a in ('dir /b C:\Users\Administrator\Desktop\快剪辑视频duzhi\*.mp4') do (
    echo put "%%a" "back_dir">> tempconfig.txt
)
echo bye >> tempconfig.txt
ftp -n -s:"tempconfig.txt"
Xcopy C:\Users\Administrator\Desktop\快剪辑视频\*.mp4 F:\backall /s /e /y
del /q C:\Users\Administrator\Desktop\快剪辑视频\*.mp4
del /q tempconfig.txt
pause

在这个bat上修改
java调用bat即可

https://www.freesion.com/article/97851323572/

看看这篇文章:https://blog.csdn.net/huaairen/article/details/87896131?spm=1005.2026.3001.5635&utm_medium=distribute.pc_relevant_ask_down.none-task-blog-2~default~OPENSEARCH~default-12.pc_feed_download_top3ask&depth_1-utm_source=distribute.pc_relevant_ask_down.none-task-blog-2~default~OPENSEARCH~default-12.pc_feed_download_top3ask

看这个网址

 

一楼的链接不就是答案,和你的提问一摸一样啊