如何在使用JSCH SFTP API創建新目錄之前檢查目錄是否存在?我正在嘗試使用lstat
,但不確定它是否在執行我需要的工作。提前感謝在創建JSCH中的新目錄之前,如何檢查目錄是否存在
5
A
回答
5
在這種情況下,最好只創建並處理錯誤。這種方式的操作是原子的,在SSH的情況下,您還可以節省大量的網絡流量。如果您先進行測試,那麼會出現一個時間窗口,在此期間情況可能會發生變化,您必須處理錯誤結果。
7
這是我如何檢查JSch中的目錄存在。
創建目錄,如果目錄不存在ONT
ChannelSftp channelSftp = (ChannelSftp)channel;
String currentDirectory=channelSftp.pwd();
String dir="abc";
SftpATTRS attrs=null;
try {
attrs = channelSftp.stat(currentDirectory+"/"+dir);
} catch (Exception e) {
System.out.println(currentDirectory+"/"+dir+" not found");
}
if (attrs != null) {
System.out.println("Directory exists IsDir="+attrs.isDir());
} else {
System.out.println("Creating dir "+dir);
channelSftp.mkdir(dir);
}
1
我在這裏重複了同樣的答案在更廣的範圍內。檢查目錄是否存在並創建新目錄的特定行是
SftpATTRS attrs;
try {
attrs = channel.stat(localChildFile.getName());
}catch (Exception e) {
channel.mkdir(localChildFile.getName());
}
注意。 localChildFile.getName()
是您要檢查的目錄名稱。下面附加了整個類,遞歸地將目錄的文件或內容發送到遠程服務器。
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* Created by krishna on 29/03/2016.
*/
public class SftpLoader {
private static Logger log = LoggerFactory.getLogger(SftpLoader.class.getName());
ChannelSftp channel;
String host;
int port;
String userName ;
String password ;
String privateKey ;
public SftpLoader(String host, int port, String userName, String password, String privateKey) throws JSchException {
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
this.privateKey = privateKey;
channel = connect();
}
private ChannelSftp connect() throws JSchException {
log.trace("connecting ...");
JSch jsch = new JSch();
Session session = jsch.getSession(userName,host,port);
session.setPassword(password);
jsch.addIdentity(privateKey);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
log.trace("connected !!!");
return (ChannelSftp)channel;
}
public void transferDirToRemote(String localDir, String remoteDir) throws SftpException, FileNotFoundException {
log.trace("local dir: " + localDir + ", remote dir: " + remoteDir);
File localFile = new File(localDir);
channel.cd(remoteDir);
// for each file in local dir
for (File localChildFile: localFile.listFiles()) {
// if file is not dir copy file
if (localChildFile.isFile()) {
log.trace("file : " + localChildFile.getName());
transferFileToRemote(localChildFile.getAbsolutePath(),remoteDir);
} // if file is dir
else if(localChildFile.isDirectory()) {
// mkdir the remote
SftpATTRS attrs;
try {
attrs = channel.stat(localChildFile.getName());
}catch (Exception e) {
channel.mkdir(localChildFile.getName());
}
log.trace("dir: " + localChildFile.getName());
// repeat (recursive)
transferDirToRemote(localChildFile.getAbsolutePath(), remoteDir + "/" + localChildFile.getName());
channel.cd("..");
}
}
}
public void transferFileToRemote(String localFile, String remoteDir) throws SftpException, FileNotFoundException {
channel.cd(remoteDir);
channel.put(new FileInputStream(new File(localFile)), new File(localFile).getName(), ChannelSftp.OVERWRITE);
}
public void transferToLocal(String remoteDir, String remoteFile, String localDir) throws SftpException, IOException {
channel.cd(remoteDir);
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(channel.get(remoteFile));
File newFile = new File(localDir);
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
log.trace("writing files ...");
int readCount;
while((readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
log.trace("completed !!!");
bis.close();
bos.close();
}
相關問題
- 1. 在創建之前檢查目錄/文件是否存在?
- 2. 如何使用ANT在刪除目錄之前檢查目錄是否存在?
- 3. 使用IsolatedStorage,創建它之前是否需要檢查目錄是否存在?
- 4. 殼創建新的目錄,並檢查它是否存在
- 5. 如何檢查目錄是否存在?
- 6. 如何檢查目錄下是否存在特定目錄?
- 7. 檢查lua中是否存在目錄?
- 8. 如何在插入文件之前檢查目錄是否存在
- 9. 檢查目錄是否不存在
- 10. Laravel s3檢查目錄是否存在。
- 11. SVNKit - 檢查目錄是否存在
- 12. Talend - 檢查目錄是否存在
- 13. NSIS檢查是否存在空目錄
- 14. 如何檢查目錄是否是Python中的臨時目錄
- 15. 檢查目錄是否存在,使目錄
- 16. 如何在Perl中檢查Windows上是否存在Unicode目錄?
- 17. 如何在shell腳本中檢查目錄是否存在?
- 18. 如何在使用rails3創建新記錄之前檢查記錄是否存在?
- 19. 在當前目錄的父目錄中創建新的目錄 - ROR FileUtils.mkdir
- 20. 如何在調用之前檢查目標是否存在?
- 21. 如何檢查Swift文檔目錄中是否存在文件?
- 22. 如何檢查目錄中是否存在文件?
- 23. 如何檢查傀儡中是否存在目錄
- 24. 如何檢查Xcode資產目錄中是否存在圖像?
- 25. 如何檢查目錄是否存在於linux中。?
- 26. 如何檢查目錄是否已經存在於MFC(VC++)中?
- 27. 如何檢查Cordova中是否存在目錄?
- 28. 如何檢查目錄中是否存在文件
- 29. 如何檢查是否一個目錄,目錄名通配符存在?
- 30. 如何檢查目錄1是否是dir2的子目錄,反之亦然
但我可以創建目錄只有一個time.After,當用戶將文件上傳到我們的應用程序到該目錄我要檢查目錄是否爲existed.So,有沒有替代的做到這一點? – SRy
@Srinivas當然,你只能創建一次。之後你會得到一個錯誤。處理它。正如我上面所說。 – EJP
是的,謝謝。我不明白那個縮頭。現在很清楚謝謝。 – SRy