JSCH

2012-11-02 9 views
5

में एक नई निर्देशिका बनाने से पहले निर्देशिका को कैसे जांचें, JSCH SFTP API का उपयोग कर नई निर्देशिका बनाने से पहले निर्देशिका के अस्तित्व की जांच कैसे करें? मैं lstat का उपयोग करने की कोशिश कर रहा हूं लेकिन यह सुनिश्चित नहीं है कि यह मेरी नौकरी कर रहा है। अग्रिम मेंJSCH

उत्तर

5

इस तरह की स्थितियों में यह हमेशा बनाना और त्रुटि को संभालना हमेशा बेहतर होता है। इस तरह ऑपरेशन परमाणु है, और एसएसएच के मामले में आप बहुत सारे नेटवर्क यातायात को भी बचाते हैं। यदि आप पहले परीक्षण करते हैं, तो एक समय खिड़की होती है जिसके दौरान स्थिति बदल सकती है, और आपको वैसे भी त्रुटि परिणामों को संभालना होगा।

+0

लेकिन मैं केवल एक निर्देशिका time.After बना सकते हैं कि जब उपयोगकर्ता उस निर्देशिका मैं चाहे निर्देशिका existed.So है की जाँच करने के लिए हमारे आवेदन करने के लिए फ़ाइल अपलोड करता है, वहाँ ऐसा करने के लिए कोई वैकल्पिक है? – SRy

+0

@ श्रीनिवास निश्चित रूप से आप इसे केवल एक बार बना सकते हैं। उसके बाद आपको एक त्रुटि मिलेगी। इसे संभालें। जैसा कि मैंने उपरोक्त कहा था। – EJP

+0

हाँ इसे मिला.धन्यवाद मैं उस eariler को समझ में नहीं आया.अब यह स्पष्ट धन्यवाद है। – SRy

7

इस प्रकार मैं JSch में निर्देशिका अस्तित्व की जांच करता हूं।

निर्देशिका बनाएं यदि dir 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); 
} 
+0

अफफ़ के लिए जैसा कि मुझे याद है कि 'attrs' शून्य नहीं होगा जब उसे कोई निर्देशिका नहीं मिलती है। यह एक अपवाद फेंकता है। – SRy

+0

@ एसआरई: हाँ, लेकिन मैंने घोषणा करते समय इसे शून्य कर दिया है, इसलिए अगर अपवाद को 'attrs' value नहीं फेंक दिया गया है तो – abi1964

+0

नहीं होगा, मुझे नहीं लगता कि आपका कोड आपके काम के अनुसार काम करता है। कारण जब उपरोक्त कोड में कोई अपवाद फेंक दिया जाता है, जब कोई निर्देशिका मौजूद नहीं होती है तो आप मानते हैं कि नियंत्रण 'अन्य' ब्लॉक पर आ जाएगा, ऐसा कोई मौका नहीं है कि नियंत्रण 'अगर-अन्य' ब्लॉक पर आता है। तो आपका कोड केवल तभी काम करता है जब 'attrs' nul – SRy

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(); 
} 
संबंधित मुद्दे