本文整理汇总了Java中org.apache.commons.net.ftp.FTPClient.makeDirectory方法的典型用法代码示例。如果您正苦于以下问题:Java FTPClient.makeDirectory方法的具体用法?Java FTPClient.makeDirectory怎么用?Java FTPClient.makeDirectory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.makeDirectory方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makeRemoteDir
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
protected void makeRemoteDir(FTPClient ftp, String dir)
throws IOException {
String workingDirectory = ftp.printWorkingDirectory();
if (dir.indexOf("/") == 0) {
ftp.changeWorkingDirectory("/");
}
String subdir;
StringTokenizer st = new StringTokenizer(dir, "/");
while (st.hasMoreTokens()) {
subdir = st.nextToken();
if (!(ftp.changeWorkingDirectory(subdir))) {
if (!(ftp.makeDirectory(subdir))) {
int rc = ftp.getReplyCode();
if (rc != 550 && rc != 553 && rc != 521) {
throw new IOException("could not create directory: " + ftp.getReplyString());
}
} else {
ftp.changeWorkingDirectory(subdir);
}
}
}
if (workingDirectory != null) {
ftp.changeWorkingDirectory(workingDirectory);
}
}
示例2: mkdirs
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Convenience method, so that we don't open a new connection when using this
* method from within another method. Otherwise every API invocation incurs
* the overhead of opening/closing a TCP connection.
*/
private boolean mkdirs(FTPClient client, Path file, FsPermission permission)
throws IOException {
boolean created = true;
Path workDir = new Path(client.printWorkingDirectory());
Path absolute = makeAbsolute(workDir, file);
String pathName = absolute.getName();
if (!exists(client, absolute)) {
Path parent = absolute.getParent();
created = (parent == null || mkdirs(client, parent, FsPermission
.getDirDefault()));
if (created) {
String parentDir = parent.toUri().getPath();
client.changeWorkingDirectory(parentDir);
created = created && client.makeDirectory(pathName);
}
} else if (isFile(client, absolute)) {
throw new ParentNotDirectoryException(String.format(
"Can't make directory for path %s since it is a file.", absolute));
}
return created;
}
示例3: uploadDirectory
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Recursively upload a directory to FTP server with the provided FTP client object.
*
* @param sourceDirectoryPath
* @param targetDirectoryPath
* @param logPrefix
* @throws IOException
*/
protected void uploadDirectory(final FTPClient ftpClient, final String sourceDirectoryPath,
final String targetDirectoryPath, final String logPrefix) throws IOException {
log.info(String.format(UPLOAD_DIR, logPrefix, sourceDirectoryPath, targetDirectoryPath));
final File sourceDirectory = new File(sourceDirectoryPath);
final File[] files = sourceDirectory.listFiles();
if (files == null || files.length == 0) {
log.info(String.format("%sEmpty directory at %s", logPrefix, sourceDirectoryPath));
return;
}
// Make sure target directory exists
final boolean isTargetDirectoryExist = ftpClient.changeWorkingDirectory(targetDirectoryPath);
if (!isTargetDirectoryExist) {
ftpClient.makeDirectory(targetDirectoryPath);
}
final String nextLevelPrefix = logPrefix + "..";
for (File file : files) {
if (file.isFile()) {
uploadFile(ftpClient, file.getAbsolutePath(), targetDirectoryPath, nextLevelPrefix);
} else {
uploadDirectory(ftpClient, Paths.get(sourceDirectoryPath, file.getName()).toString(),
targetDirectoryPath + "/" + file.getName(), nextLevelPrefix);
}
}
}
示例4: ensureDirectoryExists
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
@Override
public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException {
if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) {
ensureDirectoryExists(flowFile, directoryName.getParentFile());
}
final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", "");
final FTPClient client = getClient(flowFile);
final boolean cdSuccessful = setWorkingDirectory(remoteDirectory);
if (!cdSuccessful) {
logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory});
if (client.makeDirectory(remoteDirectory)) {
logger.debug("Created {}", new Object[] {remoteDirectory});
} else {
throw new IOException("Failed to create remote directory " + remoteDirectory);
}
}
}
示例5: MKDAndCWD
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
private boolean MKDAndCWD(FTPClient ftp, String remoteDir) throws IOException {
// 切分出所有子文件夹,按顺序
String[] dirs = remoteDir.split("/");
// 遍历文件夹
for (String subDir : dirs) {
// 文件夹字符串非空
if (!subDir.isEmpty()) {
// 切换工作目录
if (!ftp.changeWorkingDirectory(subDir)) {
// 若目录不存在则先创建
if (!ftp.makeDirectory(subDir)) {
return false;
}
// 切换工作目录
if (!ftp.changeWorkingDirectory(subDir)) {
return false;
}
}
}
}
return true;
}
示例6: uploadFileToWebApp
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Uploads a file to an Azure web app.
* @param profile the publishing profile for the web app.
* @param fileName the name of the file on server
* @param file the local file
*/
public static void uploadFileToWebApp(PublishingProfile profile, String fileName, InputStream file) {
FTPClient ftpClient = new FTPClient();
String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
String server = ftpUrlSegments[0];
String path = "./site/wwwroot/webapps";
if (fileName.contains("/")) {
int lastslash = fileName.lastIndexOf('/');
path = path + "/" + fileName.substring(0, lastslash);
fileName = fileName.substring(lastslash + 1);
}
try {
ftpClient.connect(server);
ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
for (String segment : path.split("/")) {
if (!ftpClient.changeWorkingDirectory(segment)) {
ftpClient.makeDirectory(segment);
ftpClient.changeWorkingDirectory(segment);
}
}
ftpClient.storeFile(fileName, file);
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: uploadFileToFunctionApp
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Uploads a file to an Azure function app.
* @param profile the publishing profile for the web app.
* @param fileName the name of the file on server
* @param file the local file
*/
public static void uploadFileToFunctionApp(PublishingProfile profile, String fileName, InputStream file) {
FTPClient ftpClient = new FTPClient();
String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
String server = ftpUrlSegments[0];
String path = "site/wwwroot";
if (fileName.contains("/")) {
int lastslash = fileName.lastIndexOf('/');
path = path + "/" + fileName.substring(0, lastslash);
fileName = fileName.substring(lastslash + 1);
}
try {
ftpClient.connect(server);
ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
ftpClient.setFileType(FTP.ASCII_FILE_TYPE);
for (String segment : path.split("/")) {
if (!ftpClient.changeWorkingDirectory(segment)) {
ftpClient.makeDirectory(segment);
ftpClient.changeWorkingDirectory(segment);
}
}
ftpClient.storeFile(fileName, file);
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
示例8: mkDir
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 循环创建目录,并且创建完目录后,设置工作目录为当前创建的目录下
*/
public static boolean mkDir(String ftpPath,FTPClient ftp2) {
if (!ftp2.isConnected()) {
return false;
}
try {
// 将路径中的斜杠统一
char[] chars = ftpPath.toCharArray();
StringBuffer sbStr = new StringBuffer(256);
for (int i = 0; i < chars.length; i++) {
if ('\\' == chars[i]) {
sbStr.append('/');
}else{
sbStr.append(chars[i]);
}
}
ftpPath = sbStr.toString();
//System.out.println("ftpPath" + ftpPath);
if (ftpPath.indexOf('/') == -1) {
// 只有一层目录
ftp2.makeDirectory(new String(ftpPath.getBytes(),"iso-8859-1"));
ftp2.changeWorkingDirectory(new String(ftpPath.getBytes(),"iso-8859-1"));
} else {
// 多层目录循环创建
String[] paths = ftpPath.split("/");
// String pathTemp = "";
for (int i = 0; i < paths.length; i++) {
ftp2.makeDirectory(new String(paths[i].getBytes(),"iso-8859-1"));
ftp2.changeWorkingDirectory(new String(paths[i].getBytes(), "iso-8859-1"));
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例9: createDirecroty
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 递归创建远程服务器目录
*
* @param remote 远程服务器文件绝对路径
* @param ftpClient FTPClient对象
* @return 目录创建是否成功
* @throws IOException
*/
public UploadStatus createDirecroty(String remote, FTPClient ftpClient)
throws IOException {
UploadStatus status = UploadStatus.Create_Directory_Success;
String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
if (!directory.equalsIgnoreCase("/")
&& !ftpClient.changeWorkingDirectory(new String(directory
.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET))) {
// 如果远程目录不存在,则递归创建远程服务器目录
int start;
int end;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = new String(remote.substring(start, end)
.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET);
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
} else {
//System.out.println("创建目录失败")
return UploadStatus.Create_Directory_Fail;
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return status;
}
示例10: testPathNames
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Test of obscure path names in the FTP server
*
* RFC959 states that paths are constructed thus...
* <string> ::= <char> | <char><string>
* <pathname> ::= <string>
* <char> ::= any of the 128 ASCII characters except <CR> and <LF>
*
* So we need to check how high characters and problematic are encoded
*/
public void testPathNames() throws Exception
{
logger.debug("Start testPathNames");
FTPClient ftp = connectClient();
String PATH1="testPathNames";
try
{
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login successful", login);
reply = ftp.cwd("/Alfresco/User*Homes");
assertTrue(FTPReply.isPositiveCompletion(reply));
// Delete the root directory in case it was left over from a previous test run
try
{
ftp.removeDirectory(PATH1);
}
catch (IOException e)
{
// ignore this error
}
// make root directory for this test
boolean success = ftp.makeDirectory(PATH1);
assertTrue("unable to make directory:" + PATH1, success);
success = ftp.changeWorkingDirectory(PATH1);
assertTrue("unable to change to working directory:" + PATH1, success);
assertTrue("with a space", ftp.makeDirectory("test space"));
assertTrue("with exclamation", ftp.makeDirectory("space!"));
assertTrue("with dollar", ftp.makeDirectory("space$"));
assertTrue("with brackets", ftp.makeDirectory("space()"));
assertTrue("with hash curley brackets", ftp.makeDirectory("space{}"));
//Pound sign U+00A3
//Yen Sign U+00A5
//Capital Omega U+03A9
assertTrue("with pound sign", ftp.makeDirectory("pound \u00A3.world"));
assertTrue("with yen sign", ftp.makeDirectory("yen \u00A5.world"));
// Test steps that do not work
// assertTrue("with omega", ftp.makeDirectory("omega \u03A9.world"));
// assertTrue("with obscure ASCII chars", ftp.makeDirectory("?/.,<>"));
}
finally
{
// clean up tree if left over from previous run
ftp.disconnect();
}
}
示例11: testRenameCase
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* Test of rename case ALF-20584
*
*/
public void testRenameCase() throws Exception
{
logger.debug("Start testRenameCase");
FTPClient ftp = connectClient();
String PATH1="testRenameCase";
try
{
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
fail("FTP server refused connection.");
}
boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN);
assertTrue("admin login successful", login);
reply = ftp.cwd("/Alfresco/User*Homes");
assertTrue(FTPReply.isPositiveCompletion(reply));
// Delete the root directory in case it was left over from a previous test run
try
{
ftp.removeDirectory(PATH1);
}
catch (IOException e)
{
// ignore this error
}
// make root directory for this test
boolean success = ftp.makeDirectory(PATH1);
assertTrue("unable to make directory:" + PATH1, success);
ftp.cwd(PATH1);
String FILE1_CONTENT_2="That's how it is says Pooh!";
ftp.storeFile("FileA.txt" , new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8")));
assertTrue("unable to rename", ftp.rename("FileA.txt", "FILEA.TXT"));
}
finally
{
// clean up tree if left over from previous run
ftp.disconnect();
}
}