java调用svnkit工具类上传本地文件到svn服务器
package org.jenkinsci.plugins.svn.upload.step;
import java.io.*;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
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.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class SvnUpload {
private SVNClientManager clientManager;
private ISVNAuthenticationManager authManager;
private SVNRepository repository;
/**
*
* @param svnUrl svn地址
* @param svnUsername svn用户名称
* @param svnPasswd svn用户密码
* @throws SVNException 异常信息
*/
public SvnUpload(String svnUrl, String svnUsername, String svnPasswd)throws SVNException{
try {
this.createDefaultAuthenticationManager(svnUsername, svnPasswd);
this.authSvn(svnUrl);
} catch (SVNException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 通过不同的协议初始化版本库
*/
private void setupLibrary() {
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
}
/**
*
* @param username svn用户名称
* @param password svn用户密码
* @throws SVNException 异常信息
*/
private void createDefaultAuthenticationManager(String username, String password)throws SVNException{
try {
// 身份验证
authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password);
} catch (Exception e) {
throw new RuntimeException("SVN身份认证失败:" + e.getMessage());
}
}
/**
* 验证登录svn
* @param svnUrl 用户svn的仓库地址
* @throws SVNException 异常信息
*/
private void authSvn(String svnUrl) throws SVNException {
// 初始化版本库
setupLibrary();
try {
repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnUrl));
} catch (SVNException e) {
throw new RuntimeException("SVN创建库连接失败:" + e.getMessage());
}
// 创建身份验证管理器
repository.setAuthenticationManager(authManager);
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
try {
//创建SVN实例管理器
clientManager = SVNClientManager.newInstance(options,authManager);
} catch (Exception e) {
throw new RuntimeException("SVN实例管理器创建失败:" + e.getMessage());
}
}
/**
* 添加文件和目录到版本控制下
* @param wcPath 工作区路径
* @throws SVNException 异常信息
*/
private void addEntry(File wcPath) throws SVNException{
try {
clientManager.getWCClient().doAdd(new File[] { wcPath }, true,
false, false, SVNDepth.INFINITY, false, false, true);
} catch (SVNException e) {
throw new RuntimeException("SVN添加文件到版本控制下失败:" + e.getMessage());
}
}
/**
* 将工作副本提交到svn
* @param wcPath 被提交的工作区路径
* @param keepLocks 是否在SVN仓库中打开或不打开文件
* @param commitMessage 提交信息
* @return 返回信息
* @throws SVNException 异常信息
*/
private SVNCommitInfo commit(File wcPath, boolean keepLocks, String commitMessage) throws SVNException {
try {
return clientManager.getCommitClient().doCommit(
new File[] { wcPath }, keepLocks, commitMessage, null,
null, false, false, SVNDepth.INFINITY);
} catch (SVNException e) {
throw new RuntimeException("SVN提交失败:" + e.getMessage());
}
}
/**
* 确定path是否是一个工作空间
* @param path 文件路径
* @return 返回信息
* @throws SVNException 异常信息
*/
private boolean isWorkingCopy(File path) throws SVNException{
if(!path.exists()){
return false;
}
try {
if(null == SVNWCUtil.getWorkingCopyRoot(path, false)){
return false;
}
} catch (SVNException e) {
throw new RuntimeException("确定path是否是一个工作空间 失败:" + e.getMessage());
}
return true;
}
/**
* 确定一个URL在SVN上是否存在
* @param url svn访问地址
* @return 返回信息
* @throws SVNException 异常信息
*/
private boolean isURLExist(SVNURL url) throws SVNException{
try {
SVNRepository svnRepository = SVNRepositoryFactory.create(url);
svnRepository.setAuthenticationManager(authManager);
SVNNodeKind nodeKind = svnRepository.checkPath("", -1);
return nodeKind == SVNNodeKind.NONE ? false : true;
} catch (SVNException e) {
throw new RuntimeException("确定一个URL在SVN上是否存在失败:" + e.getMessage());
}
}
/**
* 递归检查不在版本控制的文件,并add到svn
* @param wc 检查的文件
* @throws SVNException 异常信息
*/
private void checkVersiondDirectory(File wc) throws SVNException{
if(!SVNWCUtil.isVersionedDirectory(wc)){
this.addEntry(wc);
}
if(wc.isDirectory()){
for(File sub:wc.listFiles()){
if(sub.isDirectory() && sub.getName().equals(".svn")){
continue;
}
checkVersiondDirectory(sub);
}
}
}
/**
* 删除目录(文件夹)以及目录下的文件
* @param sPath 被删除目录的文件
* @return 目录删除成功返回true,否则返回false
*/
private boolean deleteDirectory(String sPath) {
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} //删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 删除单个文件
* @param sPath 被删除文件的文件
* @return 单个文件删除成功返回true,否则返回false
*/
private boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*@param sPath 要删除的目录或文件
*@return 删除成功返回 true,否则返回 false。
*/
private boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(file.getAbsolutePath());
} else { // 为目录时调用删除目录方法
return deleteDirectory(file.getAbsolutePath());
}
}
}
/**
* 更新SVN工作区
* @param wcPath 工作区路径
* @param updateToRevision 更新版本
* @param depth update的深度:目录、子目录、文件
* @return 返回信息
* @throws SVNException 异常信息
*/
private long update(File wcPath,SVNRevision updateToRevision, SVNDepth depth) throws SVNException{
SVNUpdateClient updateClient = clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
return updateClient.doUpdate(wcPath, updateToRevision,depth, false, false);
} catch (SVNException e) {
throw new RuntimeException("更新SVN工作区失败:" + e.getMessage());
}
}
/**
* SVN仓库文件检出
* @param url 文件url
* @param revision 检出版本
* @param destPath 目标路径
* @param depth checkout的深度,目录、子目录、文件
* @return 返回信息
* @throws SVNException 异常信息
*/
private long checkout(SVNURL url, SVNRevision revision, File destPath, SVNDepth depth) throws SVNException{
SVNUpdateClient updateClient = clientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
try {
return updateClient.doCheckout(url, destPath, revision, revision,depth, false);
} catch (SVNException e) {
throw new RuntimeException("检出SVN仓库失败:" + e.getMessage());
}
}
/**
* @param svnUrl svn地址
* @param workspace 工作区
* @param filepath 上传的文件地址
* @param filename 文件名称
* @throws SVNException 异常信息
*/
private void checkWorkCopy(String svnUrl,String workspace,String filepath,String filename)throws SVNException{
SVNURL repositoryURL = null;
try {
repositoryURL = SVNURL.parseURIEncoded(svnUrl);
} catch (SVNException e) {
throw new RuntimeException("解析svnUrl失败:" + e.getMessage());
}
String fPath = "";
if(filepath.indexOf("/") != -1) {
fPath = filepath.substring(0,filepath.lastIndexOf("/"));
}
File wc = new File(workspace+"/"+fPath);
File wc_project = new File( workspace + "/" + fPath);
SVNURL projectURL = null;
try {
projectURL = repositoryURL.appendPath(filename, false);
} catch (SVNException e) {
throw new RuntimeException("解析svnUrl文件失败:" + e.getMessage());
}
if(!this.isWorkingCopy(wc)){
if(!this.isURLExist(projectURL)){
this.checkout(repositoryURL, SVNRevision.HEAD, wc, SVNDepth.EMPTY);
}else{
this.checkout(projectURL, SVNRevision.HEAD, wc_project, SVNDepth.INFINITY);
}
}else{
this.update(wc, SVNRevision.HEAD, SVNDepth.INFINITY);
}
}
/**
* 循环删除.svn目录
* @param spath
*/
private void deletePointSVN(String spath){
File wc = new File(spath);
for(File sub:wc.listFiles()){
if(sub.isDirectory() && sub.getName().equals(".svn")){
this.deleteDirectory(sub.getAbsolutePath());
continue;
}
if(sub.isDirectory()){
deletePointSVN(sub.getAbsolutePath());
}
}
}
/**
*
* @param svnUrl svn地址
* @param workspace 工作区
* @param filepath 上传的文件地址
* @param filename 上传的文件名称
* @param isOverwrite 是否覆盖
* @throws SVNException 异常信息
*/
public void upload(String svnUrl,String workspace,String filepath,String filename,Boolean isOverwrite)throws SVNException{
String svnfilePath = svnUrl+"/"+filename;
//开始前删除以前的.svn文件目录
deletePointSVN(workspace);
boolean flag = this.isURLExist(SVNURL.parseURIDecoded(svnfilePath));
if(flag){
if(isOverwrite){
this.uploadFile(svnUrl, workspace, filepath,filename);
}
}else{
this.uploadFile(svnUrl, workspace, filepath,filename);
}
//结束后删除当前的.svn文件目录
deletePointSVN(workspace);
}
/**
*
* @param svnUrl svn地址
* @param workspace 工作区
* @param filepath 上传的文件地址
* @param filename 文件名称
* @throws SVNException 异常信息
*/
private void uploadFile(String svnUrl,String workspace,String filepath,String filename)throws SVNException{
this.checkWorkCopy(svnUrl, workspace, filepath,filename);
File file = new File(workspace+"/"+filepath);
this.checkVersiondDirectory(file);
this.commit(file, false, "commit file:"+file);
}
public static void main(String[] args) throws SVNException {
try {
String svnUrl = "http://192.168.18.200/svn/repository/hello";
String username = "admin";
String passwd = "admin";
String workspace = "C:\\Users\\kube\\Desktop\\test";
String upfile = "/q/w/a.txt,b.txt";
Boolean isOverwrite = true;
SvnUpload svnUpload = new SvnUpload(svnUrl,username, passwd);
String [] fileArray = upfile.split(",");
for(int i=0;i<fileArray.length;i++){
String filePath = fileArray[i];
String filename = "";
if (filePath.indexOf("/") != -1) {
filename = filePath.substring(filePath.lastIndexOf("/"));
} else {
filename = filePath;
}
svnUpload.upload(svnUrl, workspace, filePath, filename,isOverwrite);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java调用svnkit工具类上传本地文件到svn服务器的更多相关文章
- 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器
引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户后,我们就迫不及待的想用JAVA去操作Samba服务器了,我们找到了一个框架叫 jcifs,可以高效的完成我们工作. 实践: ...
- 上传本地文件到SVN
前言:今天按照自己的记忆上传本地文件夹到SVN,出现了点问题,重温了简单操作. https://blog.csdn.net/qq_35150366/article/details/81129847 参 ...
- 上传的文件放在SVN服务器的哪个目录下
SVN服务器版本库有两种格式,一种为FSFS,一种为BDB 把文件上传到SVN版本库后,上传的文件不再以文件原来的格式存储,而是被svn以它自定义的格式压缩成版本库数据,存放在版本库中. 如果是FSF ...
- git 上传本地文件到github
git 上传本地文件到github 1 git config --global user.name "Your Real Name" 2 git config --global u ...
- 两种方法上传本地文件到github
https://www.jianshu.com/p/c70ca3a02087 自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的 ...
- 两种方法上传本地文件到github(转)
自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...
- paramiko模块的安装和使用(含上传本地文件或文件夹到服务器,以及下载服务器文件到本地)
安装和使用分两步介绍: 介绍一下,本文的运行环境是win7 64位 和python 2.7 . 安装: WIN7_64位 安装python-ssh访问模块(paramiko)的安装教程,本人亲测下面 ...
- SSH 远程上传本地文件至服务器
使用SSH命令行传输文件到远程服务器 以前一直在windows下用SSH Secure Shell连接远程服务器,它自带了一个可视化的文件传输工具,跟ftp差不多 但是它也存在一个缺陷,不支持编码 ...
- 如何用一张图片代替 'input:file' 上传本地文件??
今天去面试,碰到了一道题,也许是因为紧张或者喝水喝多了,一时竟然没有转过弯来,回来之后一细想原来这么简单,哭笑不得,特此记录一下! 原题是这样的: 如何用一张图片代替 'input:file' 上传 ...
随机推荐
- cocos2dx常见Action(转)
本文转载自:http://blog.csdn.net/ff313976/article/details/23667209 bool HelloWorld::init(){ ////////////// ...
- 初步理解IOC和DI和AOP模式
初步理解IOC和DI和AOP模式 控制反转(IOC) 控制反转(IOC,Inversion of Control)是一种转主动为被动关系的一种编程模式,有点类似于工厂模式,举个栗子, 下面这个这不是I ...
- OpenStack 业务链networking-sfc介绍 (2) - 底层原理
原文链接:https://blog.csdn.net/bc_vnetwork/article/details/65630475 1. SFC底层实现原理 port chain和ovs driver/ ...
- php实现socket简单的例子
一.Socket简介 1.socket只不过是一种数据结构 2.使用这个socket数据结构去开始一个客户端和服务器之间的会话 3.服务器是一直在监听准备产生一个新的会话.当一个客户端连接服务端,它就 ...
- opencv3.3 安装环境教程以及实现个图片读取功能
一.opencv3.3安装环境 1. 首先要安装这个opencv3.3,我们必须要安装一个python (IDLE:集成开发环境),我自己安装了IDLE3.6和pycharm2017,这些软件都可以去 ...
- Generating an arbitrary digit password dictionary
原理说明:以增量方式从开始到结束! 实现方法:CMD命令 特点:纯数字 语法: FOR /L %variable IN (start,step,end) DO command [command-par ...
- jdk1.6 支持 tls1.2协议 并忽略身份验证
jdk1.6不支持tls1.2协议,jdk1.8默认支持,比较好的解决方案是升级jdk,但是升级jdk风险极大.不能升级jdk的情况下,可以使用如下方式. 引入依赖 <dependency> ...
- [XHR]——重新认识 XMLHttpRequest
细说XMLHttpRequest如何使用 先来看一段使用XMLHttpRequest发送Ajax请求的简单示例代码. function sendAjax() { //构造表单数据 var formDa ...
- eclipse的classpath(build path)和classpaht几种设置的方式
1,默认eclipse有自己的classpath的路径并不是环境变量中配置的classpah. 2,eclipse的classpath每个项目不同,一般是在工作区的当前项目的class下. 2.1,可 ...
- 关于在JS中AJAX导致跨域问题的解决
在部署一个原声的前端项目的时候,请求该服务器后端接口时发现出现了CORS跨域的问题,但是服务端已经做了同源策略的兼容,常见问题,遂记录. 报错信息: XMLHttpRequest cannot loa ...