2014-10-08 46 views
0

SVN服務器可通過https訪問。因此,我需要複製位於那裏的整個文件夾,並將其存儲在本地計算機的臨時文件夾中。目前,我編寫了一個代碼,可以讀取位於特定路徑中的文件內容,並在出現錯誤消息時夾。通過https從SVN複製目錄

我也想複製位於SVN特定路徑中的整個文件夾到我的本地機器的臨時文件夾中,並在遇到文件時給出錯誤消息。我試圖使用方法「結賬」添加代碼此功能,但不明白我要去哪裏wrong.Please幫助..

我的Java代碼..

import java.io.IOException; 
import java.io.ByteArrayOutputStream; 
import org.tmatesoft.svn.core.*; 
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; 
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; 
import org.tmatesoft.svn.core.io.SVNRepository; 
import org.tmatesoft.svn.core.io.SVNRepositoryFactory; 
import org.tmatesoft.svn.core.wc.SVNWCUtil; 

public class SVNRepoConnector { 

private String username = "lucy"; 
private String password = "windows"; 
private String baseUrl = "https://sun-wdsvn1.apple.org/svn/CPS/GDD/trunk"; 
private String filePath = "pom.xml"; 

public void downloadSchema() { 
    DAVRepositoryFactory.setup(); 
    SVNRepository repository = null; 

    try { 
     repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(baseUrl)); 
     ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password); 
     repository.setAuthenticationManager(authManager); 

     SVNNodeKind nodeKind = repository.checkPath(filePath, -1); 

     if(nodeKind == SVNNodeKind.NONE) { 
      System.err.println("There is file at: " + baseUrl + filePath); 
      System.exit(1); 
     } else if (nodeKind == SVNNodeKind.DIR) { 
      System.err.println("The entry at " + baseUrl + filePath + " is a directory while a file was expected."); 
      System.exit(1); 
     } 

     SVNProperties properties = new SVNProperties(); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     repository.getFile(filePath, -1, properties, out); 

     System.out.println("Content:\n"); 
     try { 
      out.writeTo(System.out); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (SVNException e) { 
     e.printStackTrace(); 
    } 
    checkout("https://sun-wdsvn1.apple.org/svn/CPS/GDD/trunk","4521","C:\temp") 
} 

public static long checkout(SVNURL url, 
     SVNRevision revision, File destPath, boolean isRecursive) 
     throws SVNException { 

    SVNUpdateClient updateClient = ourClientManager.getUpdateClient(); 
    /* 
    * sets externals not to be ignored during the checkout 
    */ 
    updateClient.setIgnoreExternals(false); 
    /* 
    * returns the number of the revision at which the working copy is 
    */ 
    return updateClient.doCheckout(url, destPath, revision, revision, isRecursive); 
} 

public static void main(String[] args) { 
    SVNRepoConnector connector = new SVNRepoConnector(); 
    connector.downloadSchema(); 
} 

}

回答

0

恕我直言,它更容易使用svn命令行推出的形式,你的java代碼

對於副本回購工作

//svn co 

p = Runtime.getRuntime().exec("svn co " + baseUrl); 
p.waitFor(); 

BufferedReader reader = 
    new BufferedReader(new InputStreamReader(p.getInputStream())); 

String line = "";   
while ((line = reader.readLine())!= null) { 
sb.append(line + "\n"); 
} 
+0

我需要在本地機器的特定位置存儲特定文件夾的工作副本。您的代碼是否執行此操作? – Lucy 2014-10-08 10:50:12

+0

是的,它只是檢查你的項目與svn特定的命令外殼「svn公司[URL]」,然後複製它在你的特定位置 – Rotka 2014-10-08 12:25:57

+0

好的。如果它不是太麻煩,請你檢查我寫的Java方法在上面的代碼中告訴我我做錯了什麼。 – Lucy 2014-10-08 13:04:24