使用Java中的JSch在服务器上创建嵌套目录 [英] Creating nested directories on server using JSch in Java
本文介绍了使用Java中的JSch在服务器上创建嵌套目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 jSch
在Java中创建文件上传应用程序。我想根据创建日期等将我的文件放在不同的目录中。
I am making an application for file uploading in Java using jSch
. I want to put my file in different directories based on their creation date etc.
我有一个主目录/ var / local / recordingsbackup /
,其中我正在创建其他目录并将数据放入其中。
I have a main directory "/var/local/recordingsbackup/"
in which I am creating other directories and putting data in them.
实现此目的:
- 我必须创建Dir'y,如
/ var / local / recordingsbackup / 20140207 / root / SUCCESS / WN /
并将
数据放入其中。
到目前为止我已经尝试过这个:
I've tried this so far:
private void fileTransfer(ChannelSftp channelTarget, temp_recording_log recObj, String filePath) {
int fileNameStartIndex = filePath.lastIndexOf("/") + 1;
String date = new SimpleDateFormat("yyyyMMdd").format(recObj.getCalldate());
String fileName = filePath.substring(fileNameStartIndex);
String staticPath = "/var/local/recordingsbackup/";
String completeBackupPath = staticPath + date + "/" + recObj.getUsername() + "/" + recObj.getStatus() + "/" + recObj.getDisposition() + "/";
try {
InputStream get = SourceChannel.get(filePath);
try {
channelTarget.put(get, completeBackupPath + fileName);
} catch (SftpException e) {
System.out.println("Creating Directory...");
channelTarget.mkdir(completeBackupPath); // error on this line
channelTarget.put(get, completeBackupPath + fileName);
}
} catch (SftpException e) {
log.error("Error Occured ======== File or Directory dosen't exists === " + filePath);
e.printStackTrace();
}
}
- 如果我是创建单个目录,如
/ var / local / recordingsbackup /
,然后不会发生错误,文件也成功上传。 - If I'm creating single dir like
/var/local/recordingsbackup/
then no error occurs and files successfully uploaded.
请帮助我...我怎样才能创建这些嵌套目录???
Please help me in this...how can I create these Nested Directories???
推荐答案
最后,我已经完成了。
这就是我成功的方式:
try {
channelTarget.put(get, completeBackupPath + fileName);
} catch (SftpException e) {
System.out.println("Creating Directory...");
String[] complPath = completeBackupPath.split("/");
channelTarget.cd("/");
for (String dir : complPath) {
if (folder.length() > 0) {
try {
System.out.println("Current Dir : " + channelTarget.pwd());
channelTarget.cd(folder);
} catch (SftpException e2) {
channelTarget.mkdir(folder);
channelTarget.cd(folder);
}
}
}
channelTarget.cd("/");
System.out.println("Current Dir : " + channelTarget.pwd());
channelTarget.put(get, completeBackupPath + fileName);
}
这篇关于使用Java中的JSch在服务器上创建嵌套目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文