FTPClient文件上传问题

问题:
1、FTPClient文件上传方法storeFile(remoteFilePath, input)不会自动创建文件夹。
2、FTPClient创建文件夹方法makeDirectory(dirName)不支持创建多层级文件夹。
比如:文件路径:/C6666666/2019-06-12/20190612013246.jpg 
直接调用storeFile(remoteFilePath, input)方法进行文件上传,
如果存在/C6666666/2019-06-12文件夹,则上传成功,
如果本身不存在这个层级文件夹,则会抛出异常。
试想,调用makeDirectory(dirName)方法去创建该多层级文件夹,结果会抛出异常。
解决方案:

多层级文件夹路径切割成单层级文件夹,如果该文件夹不存在,则进行创建。文件夹都存在的情况下上传文件是可以成功的。

 //在ftp服务中创建文件夹,如果存在则不创建 
 //remoteFilePath:/C6666666/2019-06-12/20190612013246.jpg
 String[] fileNames = remoteFilePath.split("/");
 for (Integer i = 0; i < fileNames.length - 1; i++) {
 	  //创建文件夹,不支持创建多层级文件夹。
      ftp.makeDirectory(fileNames[i]);
      //改变工作目录,即使用刚创建成功的目录
      ftp.changeWorkingDirectory(fileNames[i]);
 }
 //上传文件
 if(!existFile(ftp,remoteFilePath)){
 	  //upload 封装了storeFile(remoteFilePath, input)方法
      upload(ftp,fileNames[fileNames.length - 1], localFilePath);
 }else{
     System.out.println("[存在]:"+remoteFilePath);
 }

要是还有不太明白的地方请留言,评论必回
要是对我的文章感兴趣的话,关注一下吧,谢谢!

上一篇:ftp客户端往服务端上传文件

下一篇:Linux基本命令

猜你喜欢

转载自blog.csdn.net/chen_2890/article/details/93418673