Java,通过自动扫描本地文件夹下所有的TXT和zip文件,然后通过FTP上传到目标地址下,两个地址都是动态的,不是写死的。需要源码和相关依赖配置
5条回答 默认 最新
- threenewbee 2020-09-17 20:18关注
方法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即可解决 无用评论 打赏 举报
悬赏问题
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?
- ¥15 c++头文件不能识别CDialog