| | |
| | | package com.whyc.dto; |
| | | |
| | | import ch.qos.logback.core.joran.spi.InterpretationContext; |
| | | import com.enterprisedt.net.ftp.FTPClient; |
| | | import com.enterprisedt.net.ftp.FTPConnectMode; |
| | | import com.enterprisedt.net.ftp.FTPTransferType; |
| | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | |
| | | public class FtpHelper { |
| | | private FTPClient ftp; |
| | |
| | | public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) { |
| | | ftp = new FTPClient(); |
| | | try { |
| | | ftp.setControlEncoding("UTF-8"); |
| | | ftp.setControlEncoding("GBK"); |
| | | ftp.setRemoteHost(ftpServer); |
| | | ftp.setRemotePort(ftpPort); |
| | | ftp.setTimeout(6000); |
| | |
| | | String type = filePath.substring(0, len); |
| | | String[] typeArray = type.split("/"); |
| | | for (String s : typeArray) { |
| | | s.trim(); |
| | | if (!checkDirectory(ftp, s)) { |
| | | ftp.mkdir(s); |
| | | changeDirectory(s); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | //读取文件列表 |
| | | public List<File> getFileList(String path) { |
| | | List<File> rsList = new ArrayList<>(); |
| | | if (path == null || path.equals("")) { |
| | | return rsList; |
| | | } |
| | | File file = new File(path); |
| | | if (!file.exists()) { |
| | | return rsList; |
| | | } |
| | | // 处理文件 |
| | | if (file.isFile()) { |
| | | rsList.add(file); |
| | | return rsList; |
| | | } |
| | | // 处理文件夹 |
| | | if (null == file.listFiles()) { |
| | | rsList.add(file); |
| | | return rsList; |
| | | } |
| | | LinkedList<File> forDealList = new LinkedList<>(); |
| | | forDealList.addAll(Arrays.asList(file.listFiles())); |
| | | while (!forDealList.isEmpty()) { |
| | | File firstFile = forDealList.removeFirst(); |
| | | // 处理文件 |
| | | if (firstFile.isFile()) { |
| | | // 把文件自身先加入结果 |
| | | rsList.add(firstFile); |
| | | continue; |
| | | } |
| | | // 处理文件夹 |
| | | File[] files = firstFile.listFiles(); |
| | | if (files == null) { |
| | | continue; |
| | | } |
| | | for (File curr : files) { |
| | | if (curr.isDirectory()) { |
| | | // 将此文件夹放入待处理列表 |
| | | forDealList.add(curr); |
| | | } else { |
| | | rsList.add(curr); |
| | | } |
| | | } |
| | | } |
| | | return rsList; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | try { // 从ftp下载文件 |
| | | FtpHelper ftp = new FtpHelper("192.168.10.80", 21, "lxw", "lxw810412026"); |
| | | File file = new File("D:\\IDEAWorkSpace\\CadDrawManager\\target\\face.zip"); |
| | | ftp.uploadFile(file, "test/lxw/face.zip"); |
| | | ftp.disconnect(); |
| | | //File file = new File("D:\\fileTest\\内阻分析软件-问题反馈-20221031.docx"); |
| | | /*ftp.uploadFile(file, "test/"+"内阻分析软件-问题反馈-20221031.docx"); |
| | | ftp.disconnect();*/ |
| | | List<File> list=ftp.getFileList("D:\\fileTest\\software"); |
| | | if(list!=null&&list.size()>0){ |
| | | for (File file:list) { |
| | | String name=file.getPath().substring(file.getPath().lastIndexOf("software")); |
| | | System.out.println(name); |
| | | String pathName="/"+name.replace("\\","/"); |
| | | ftp.uploadFile(file, pathName); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |