使用svnkit通过https从SVN读取文件

时间:2014-04-16 09:42:56

标签: java svnkit

可以通过https访问SVN服务器。所以我需要读取位于那里的文件。我关注了svnkit wiki(http://svn.svnkit.com/repos/svnkit/tags/1.3.5/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayFile.java)的片段,但我的SVNKindNodeNONE,因此没有读取文件。然而,连接期间没有例外。所以我可以假设我确实正确连接到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等于NONE。我无法理解这里有什么问题。如何通过https从SVN读取文件?

不过,我的svnkit是1.8.5。

2 个答案:

答案 0 :(得分:0)

指定相对路径(除非baseUrl是存储库根目录):

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

而不是

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

答案 1 :(得分:0)

我在彻底调试了源码后找到了解决方案。

简而言之,问题在于repository.checkPath(filePath, -1);repository.getFile(filePath, -1, properties, out);的第二个参数。 filePath必须是文件名,其路径必须位于baseUrl字段中。在这些改变之后,一切都开始正常工作。

关于代码段,如果是www/license.html,则应将1作为第二个arg传递。