FTP 路径有关问题

FTP 路径有关问题

FTP 路径问题

上传文件到ftp服务器,如果知道绝对路径,就用:

 

			status = this.CreateDirecroty(realRemoteDir, ftpClient);
			if (status != UploadStatus.Create_Directory_Success) {
				return status;
			}

 如果不知道绝对路径,就把以上这几行代码屏蔽。

 

	/**
	  * CreateDirecroty(递归创建远程服务器目录)
	  * @param remoteFileDir 远程服务器文件绝对路径
	  * @param ftpClient  FTPClient对象
	  * @return 目录创建是否成功
	  * @throws IOException e
	  */
	public UploadStatus CreateDirecroty(String remoteFileDir,
			FTPClient ftpClient) throws IOException {
		UploadStatus status = UploadStatus.Create_Directory_Success;
		String directory = remoteFileDir;

		if (!directory.equalsIgnoreCase(File.separator)
				&& !ftpClient.changeWorkingDirectory(new String(directory
						.getBytes(this.ftpConfig.getDefaultLocalCharset()),
						this.ftpConfig.getDefaultRemoteCharset()))) {
			// 如果远程目录不存在,则递归创建远程服务器目录
			int maxIndex = MAX_INDEX;// 最大创建层级数
			int start = 0;
			int end = 0;
			if (directory.startsWith(File.separator)) {
				start = 1;
			} else {
				start = 0;
			}
			end = directory.indexOf(File.separator, start);
			while (true) {
				String subDirectory = new String(remoteFileDir.substring(start,
						end).getBytes(this.ftpConfig.getDefaultLocalCharset()),
						this.ftpConfig.getDefaultRemoteCharset());
				if (!ftpClient.changeWorkingDirectory(subDirectory)) {
					if (ftpClient.makeDirectory(subDirectory)) {
						ftpClient.changeWorkingDirectory(subDirectory);
					} else {
						logger.error("创建目录失败");
						return UploadStatus.Create_Directory_Fail;
					}
				}

				start = end + 1;
				end = directory.indexOf(File.separator, start);

				// 检查所有目录是否创建完毕
				if (end <= start) {
					break;
				}
				if (maxIndex == 0)
					break;

				maxIndex--;
			}
		}
		return status;
	}