| | |
| | | package com.whyc.dto; |
| | | |
| | | import com.enterprisedt.net.ftp.FTPClient; |
| | | import com.enterprisedt.net.ftp.FTPConnectMode; |
| | | import com.enterprisedt.net.ftp.FTPTransferType; |
| | | import com.whyc.constant.YamlProperties; |
| | | import com.enterprisedt.net.ftp.*; |
| | | import org.apache.commons.lang.StringUtils; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.io.*; |
| | | import java.text.ParseException; |
| | | 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); |
| | | ftp.setTimeout(1000*60*60*12); |
| | | ftp.setConnectMode(FTPConnectMode.ACTIVE); |
| | | ftp.connect(); |
| | | ftp.login(ftpUsername, ftpPassword); |
| | |
| | | ftp.chdir(dirName); |
| | | flag = true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | //e.printStackTrace(); |
| | | flag = false; |
| | | } |
| | | return flag; |
| | |
| | | String type = filePath.substring(0, len); |
| | | String[] typeArray = type.split("/"); |
| | | for (String s : typeArray) { |
| | | s.trim(); |
| | | if (!checkDirectory(ftp, s)) { |
| | | ftp.mkdir(s); |
| | | if(!ftp.existsDirectory(s)) { |
| | | ftp.mkdir(s); |
| | | } |
| | | changeDirectory(s); |
| | | } |
| | | } |
| | | } |
| | | ftp.put(inStream, fileName); |
| | | changeDirectory("/"); |
| | | } |
| | | |
| | | public void uploadFile2(File inFile,String fileName) throws IOException, FTPException { |
| | | InputStream inStream = new FileInputStream(inFile); |
| | | ftp.put(inStream, fileName); |
| | | } |
| | | |
| | |
| | | ftp.delete(fileName); |
| | | } |
| | | |
| | | /* |
| | | 切换目录 |
| | | |
| | | //切换目录 |
| | | public void changeDirectory(String path) { |
| | | if (!ValidateUtils.isEmpty(path)) { |
| | | if (!StringUtils.isEmpty(path)) { |
| | | try { |
| | | ftp.chdir(path); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }*/ |
| | | public static void main(String[] args) { |
| | | try { // 从ftp下载文件 |
| | | FtpHelper ftp = new FtpHelper("192.168.10.80", 21, "lxw", "lxw810412026"); |
| | | File file = new File("D:\\1.doc"); |
| | | ftp.uploadFile(file, "test1/1.doc"); |
| | | ftp.disconnect(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | //读取文件列表 |
| | | 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 void deleteDir(String dirName) throws IOException, FTPException, ParseException { |
| | | FTPClient client = ftp; |
| | | |
| | | //String[] dir = client.dir(dirName, true); |
| | | FTPFile[] ftpFiles = client.dirDetails(dirName); |
| | | for (FTPFile ftpFile : ftpFiles) { |
| | | if(ftpFile.isDir()){ |
| | | deleteDir(dirName+"/"+ftpFile.getName()); |
| | | }else{ |
| | | client.delete(dirName+"/"+ftpFile.getName()); |
| | | } |
| | | } |
| | | /*for (String subDirName : dir) { |
| | | String name = subDirName.substring(subDirName.lastIndexOf(" ") + 1); |
| | | if(subDirName.contains("<DIR>")){ //文件夹 |
| | | //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK"); |
| | | deleteDir(dirName+"/"+name); |
| | | //client.rmdir(dirName+"/"+name); |
| | | }else{ |
| | | //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK"); |
| | | client.delete(dirName+"/"+name); |
| | | } |
| | | }*/ |
| | | client.rmdir(dirName); |
| | | } |
| | | |
| | | public String[] getDirList() throws IOException, FTPException { |
| | | return ftp.dir(); |
| | | } |
| | | |
| | | public void mkdir(String dirPath) throws IOException, FTPException { |
| | | ftp.mkdir(dirPath); |
| | | } |
| | | } |