SVN可视化平台(2) -- svnkit判断文件是否存在

avatar 2021年12月20日10:44:47 6 2248 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

续接上文

本文介绍 svnkit 判断文件是否存在

直接上代码

import lombok.extern.slf4j.Slf4j;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/**
 * @author liuyanzhao
 * @date 2021/12/20 10:02
 */
@Slf4j
public class SVNUtilDemo
{

    public static ISVNOptions isvnOptions;

    // 初始化
    static
    {
        //1.根据访问协议初始化工厂
        DAVRepositoryFactory.setup();
        //2.使用默认选项
        isvnOptions = SVNWCUtil.createDefaultOptions(true);
    }


    /**
     * 方法一、检查文件是否存在
     *
     * @param username 用户名
     * @param password 密码
     * @param fileUrl  文件URL
     * @return
     */
    public static boolean fileIsExist(String username, String password, String fileUrl)
    {
        ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
        // 创建SVNClientManager的实例
        SVNClientManager svnClientManager = SVNClientManager.newInstance(isvnOptions, isvnAuthenticationManager);
        try
        {
            svnClientManager.getWCClient().doInfo(SVNURL.parseURIEncoded(fileUrl), SVNRevision.create(-1), SVNRevision.create(-1));
        } catch (SVNException e)
        {
            return false;
        }
        return true;
    }


    /**
     * 方法二、检查文件是否存在
     *
     * @param username 用户名
     * @param password 密码
     * @param repoUrl  仓库url
     * @param path     文件路径
     * @return
     */
    public static boolean fileIsExist(String username, String password, String repoUrl, String path)
    {
        ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
        // 创建SVNClientManager的实例
        SVNClientManager svnClientManager = SVNClientManager.newInstance(isvnOptions, isvnAuthenticationManager);
        try
        {
            SVNDirEntry dirEntry = svnClientManager.createRepository(SVNURL.parseURIEncoded(repoUrl), true)
                    .info(path, -1);

            if (dirEntry == null)
            {
                return false;
            }
        } catch (SVNException e)
        {
            return false;
        }
        return true;
    }


    /**
     * 功能说明:
     * 判断文件是否存在
     */
    public static void main(String[] args) throws Exception
    {
        String username = "admin"; // svn账号
        String password = "123456"; // svn密码
        String repoUrl = "http://127.0.0.1/svn/项目"; // 仓库地址
        String path = "/新项目测试1/开发库"; // 文件路径
        // 这2种方法都可以
        fileIsExist(username, password, repoUrl, path);
//        fileIsExist(username, password, repoUrl + path);
    }

}
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0