| | |
| | | @Slf4j |
| | | public class FtpUtil { |
| | | public FTPClient ftpClient = new FTPClient(); |
| | | public static String hostname; |
| | | public static int port; |
| | | public static String username; |
| | | public static String password; |
| | | |
| | | public FtpUtil() { |
| | | // 设置将过程中使用到的命令输出到控制台 |
| | |
| | | public FtpUtil(String hostname, int port, String username, String password) |
| | | throws IOException { |
| | | // 设置将过程中使用到的命令输出到控制台 |
| | | connect(hostname, port, username, password); |
| | | FtpUtil.hostname =hostname; |
| | | FtpUtil.port =port; |
| | | FtpUtil.username =username; |
| | | FtpUtil.password =password; |
| | | connect(); |
| | | } |
| | | |
| | | /** */ |
| | | /** |
| | | * 连接到FTP服务器 |
| | | * |
| | | * @param hostname |
| | | * 主机名 |
| | | * @param port |
| | | * 端口 |
| | | * @param username |
| | | * 用户名 |
| | | * @param password |
| | | * 密码 |
| | | * @return 是否连接成功 |
| | | * @throws IOException |
| | | */ |
| | | public boolean connect(String hostname, int port, String username, |
| | | String password) throws IOException { |
| | | public boolean connect() throws IOException { |
| | | ftpClient.connect(hostname, port); |
| | | ftpClient.setControlEncoding("GBK"); |
| | | if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { |
| | |
| | | ftpClient.setControlEncoding("GBK"); |
| | | // 对远程目录的处理 |
| | | String remoteFileName = remote; |
| | | if (remote.contains(File.separator)) { |
| | | remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1); |
| | | if (remote.contains("/")) { |
| | | remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); |
| | | // 创建服务器远程目录结构,创建失败直接返回 |
| | | if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { |
| | | log.error("创建ftp目录失败======================="+remote); |
| | |
| | | String result; |
| | | // 对远程目录的处理 |
| | | String remoteFileName = remote ; |
| | | if (remote.contains(File.separator)) { |
| | | remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1); |
| | | if (remote.contains("/")) { |
| | | remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); |
| | | // 创建服务器远程目录结构,创建失败直接返回 |
| | | if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { |
| | | return "2"; |
| | |
| | | throws IOException { |
| | | String status = "1"; |
| | | // UploadStatus status = UploadStatus.Create_Directory_Success; |
| | | String directory = remote.substring(0, remote.lastIndexOf(File.separator) + 1); |
| | | if (!directory.equalsIgnoreCase(File.separator) |
| | | String directory = remote.substring(0, remote.lastIndexOf("/") + 1); |
| | | if (!directory.equalsIgnoreCase("/") |
| | | && !ftpClient.changeWorkingDirectory(new String(directory |
| | | .getBytes("GBK"), "iso-8859-1"))) { |
| | | // 如果远程目录不存在,则递归创建远程服务器目录 |
| | | int start = 0; |
| | | int end = 0; |
| | | if (directory.startsWith(File.separator)) { |
| | | if (directory.startsWith("/")) { |
| | | start = 1; |
| | | } else { |
| | | start = 0; |
| | | } |
| | | end = directory |
| | | .indexOf(File.separator, start); |
| | | .indexOf("/", start); |
| | | while (true) { |
| | | String subDirectory = new String(remote.substring(start, end) |
| | | .getBytes("GBK"), "iso-8859-1"); |
| | |
| | | } |
| | | |
| | | start = end + 1; |
| | | end = directory.indexOf(File.separator, |
| | | end = directory.indexOf("/", |
| | | start); |
| | | |
| | | // 检查所有目录是否创建完毕 |
| | |
| | | public static void main(String[] args) throws Exception { |
| | | try { |
| | | FtpUtil ftpUtil = new FtpUtil("175.27.187.84", 21, "ftpuser","doumee168" ); |
| | | ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+File.separator, UUID.randomUUID().toString()+"test.jpg"); |
| | | ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+"/", UUID.randomUUID().toString()+"test.jpg"); |
| | | /*FtpUtil myFtp = new FtpUtil("106.15.54.228", 21, "ftptlg", |
| | | "tlg168.com"); |
| | | System.out.println(myFtp.upload("D:\\devices.sql", "/1111/devices.sql")); |
| | |
| | | String remoteDirectoryPath) throws Exception { |
| | | File src = new File(localDirectory); |
| | | try { |
| | | remoteDirectoryPath = remoteDirectoryPath + File.separator; |
| | | remoteDirectoryPath = remoteDirectoryPath + "/"; |
| | | boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath); |
| | | }catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | String tPath = remoteDirectoryPath+f.getName(); |
| | | if (!f.isDirectory()) { |
| | | |
| | | int dNum = getNumFromStr(tPath,File.separator.toCharArray()[0]); |
| | | int dNum = getNumFromStr(tPath,"/".toCharArray()[0]); |
| | | uploadFile(f, tPath); |
| | | if(dNum -2>=0){ |
| | | for (int i = 0; i < dNum-1; i++) { |
| | |
| | | } |
| | | }else{ |
| | | uploadDirectory(srcName, |
| | | tPath+File.separator); |
| | | tPath+"/"); |
| | | } |
| | | } |
| | | return true; |
| | |
| | | * */ |
| | | public boolean delDirectory(String pathName ) throws Exception { |
| | | try { |
| | | this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf(File.separator)) ); |
| | | this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf("/")) ); |
| | | ftpClient.removeDirectory(pathName); |
| | | }catch (IOException e) { |
| | | e.printStackTrace(); |