2014-04-16 90 views
0

SVN服務器可通過https訪問。所以我需要讀取一個位於那裏的文件。我跟着從svnkit維基(http://svn.svnkit.com/repos/svnkit/tags/1.3.5/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayFile.java)的片段,但我的SVNKindNodeNONE,因此沒有文件被讀取。儘管如此,連接過程中也沒有例外。所以我可以假設我確實正確連接到SVN服務器,但出現問題。使用svnkit通過https從SVN中讀取文件

下面是代碼:

public class SVNRepoConnector { 
    private String username = "user"; 
    private String password = "pwd"; 
    private String baseUrl = "https://mysvnserver.com/svn/project/trunk"; 
    private String filePath = "/myproject/src/main/webapp/file.html"; 

    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(); 
     } 
    } 

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

我收到 「有是在文件...」 由於SVNNodeKind等於無。我無法理解這裏出了什麼問題。如何通過https從SVN中讀取文件?

順便說一句,我svnkit是1.8.5。

+0

你可以檢查一個公共文件/服務器,所以它可以重現其他人嗎? – mstrap

+0

是的,我甚至試圖從svnkit(鏈接在消息中)和結果是相同的(他們在例子中使用自己的公共svn)。 – Dragon

+0

http://svn.svnkit.com/repos/svnkit/trunk/www/license.html給出了404.我很確定你的情況,URL或文件路徑是錯誤的。您在訪問瀏覽器中的完整網址時是否可以看到該文件? – mstrap

回答

0

我發現的來源徹底調試後的溶液。

總之,問題出在第二個參數repository.checkPath(filePath, -1);repository.getFile(filePath, -1, properties, out);filePath必須是文件名和路徑必須位於baseUrl字段中。這些更改後,所有事情都開始正常工作

關於該片段,如果是www/license.html,則應該通過1作爲第二參數。

0

指定相對路徑(除非baseUrl是存儲庫根):的

private String filePath = "myproject/src/main/webapp/file.html"; 

代替

private String filePath = "/myproject/src/main/webapp/file.html";