栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 操作svn 使用svnkit进行新增上传、更新、删除文件等操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 操作svn 使用svnkit进行新增上传、更新、删除文件等操作

Java 操作svn 使用svnkit进行新增上传、更新、删除文件等操作

最近在做svn集成开发,所以记录一下。
用到了svnkit进行二次开发,达到项目需求。
官网:https://svnkit.com/
api:https://svnkit.com/javadoc/index.html

只写了以下几点:
1.新建文件夹
2.增加文件
3.修改文件
4.删除文件 或文件夹

有需要的可以根据自己的项目需求进行修改

下面直接上代码

package com.ruoyi.web.controller.demo.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class SvnKitUtils {
	
	private static SVNRepository svnRepository;
	private static long revisionNo = -1; //指定版本号为最新版本
	private static ISVNEditor editor;
	
	
	public SvnKitUtils(String url,String username,String password) throws SVNException {
		    char[] pwd = password.toCharArray();
		    svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
	        ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd);
	        svnRepository.setAuthenticationManager(isvnAuthenticationManager);
	}
	
	
    private static SVNCommitInfo addFile(ISVNEditor editor,long revisionNo, String dirPath , String fileName , byte[] data) throws SVNException{
    	editor.openRoot(revisionNo);
        editor.openDir(dirPath,revisionNo);
        editor.addFile(fileName,null,revisionNo);
        editor.applyTextDelta(fileName,null);
        SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
        String checksum = deltaGenerator.sendDelta( fileName , new ByteArrayInputStream( data ) , editor , true );
        editor.closeFile(fileName, checksum);
        editor.closeDir();
        editor.closeDir();
        return editor.closeEdit();
    }
    
    
    @SuppressWarnings("unused")
	private static SVNCommitInfo addDir(ISVNEditor editor,long revisionNo, String dirPath , String fileDirName) throws SVNException{
    	editor.openRoot(revisionNo);
        editor.openDir(dirPath,revisionNo);
        editor.addDir(fileDirName, null, revisionNo);
        editor.closeDir();
        editor.closeDir();
        return editor.closeEdit();
    }
    
    
    private static SVNCommitInfo modifyFile(ISVNEditor editor,String dirPath , String fileName , byte[] oldData , byte[] newData) throws SVNException {
	      editor.openRoot( -1 );
          editor.openDir( dirPath , -1 );
          editor.openFile( fileName , -1 );
          editor.applyTextDelta( fileName , null );
          SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator( );
          String checksum = deltaGenerator.sendDelta( fileName , new ByteArrayInputStream( oldData ) , 0 , new ByteArrayInputStream( newData ) , editor , true );
          editor.closeFile( fileName , checksum );
          editor.closeDir( );
          editor.closeDir( );
          return editor.closeEdit( );
      }
	
	
    private static SVNCommitInfo deleteFile(ISVNEditor editor,String dirPath,long revisionNo) throws SVNException{
        // 进入Root节点
        editor.openRoot(revisionNo);
        //4.3.删除文件
        editor.deleteEntry(dirPath,revisionNo);
        //操作完成要关闭编辑器,并返回操作结果
        return editor.closeEdit();
    }

	
	 public  byte[] file2Byte(String filePath) {
	        byte[] fileBytes = null;
	        FileInputStream fis = null;
	        try {
	            File file = new File(filePath);
	            fis = new FileInputStream(file);
	            fileBytes = new byte[(int) file.length()];
	            fis.read(fileBytes);
	            fis.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return fileBytes;
	    }

	 @SuppressWarnings("static-access")
	public static void main(String[] args) {
		 String url = "http://localhost:81/repo/";
		 String username = "用户名称";
	     String password = "密码";
		 //1.新建文件夹
		 try {
			 SvnKitUtils svn = new SvnKitUtils(url, username, password);
				ISVNEditor editor = svn.svnRepository.getCommitEditor("新建文件夹",null,true,null);
			    SVNCommitInfo svnCommitInfo = svn.deleteFile(editor, "/snv/开发类/a/itemC1.txt", revisionNo);
				System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
			} catch (SVNException e) {
				e.printStackTrace();
			}
		 //2.增加文件
//		 try {
//			 SvnKitUtils svn = new SvnKitUtils(url, username, password);
//			byte[] newData = "测试-----测试-----22222222222".getBytes();
//			ISVNEditor editor = svn.svnRepository.getCommitEditor("增加文件",null,true,null);
			svn.addFile(editor,revisionNo, "/snv/开发类/a", "itemC1.txt", newData);
//			System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
//		} catch (SVNException e) {
//			e.printStackTrace();
//		}
		 //3.修改文件
//	 try {
//		 SvnKitUtils svn = new SvnKitUtils(url, username, password);
//			byte[] oldData = "测试-----测试-----22222222222".getBytes();
//			byte[] newData = "测试-----测试-----33333333333".getBytes();
//			ISVNEditor editor = svn.svnRepository.getCommitEditor("修改文件",null,true,null);
			svn.modifyFile(editor, "/snv/开发类/a", "/snv/开发类/a/itemC1.txt", oldData, newData);
//			System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
//		} catch (SVNException e) {
//			e.printStackTrace();
//		}
	 //4.删除文件  或文件夹
//	 try {
//		 SvnKitUtils svn = new SvnKitUtils(url, username, password);
//			ISVNEditor editor = svn.svnRepository.getCommitEditor("删除文件",null,true,null);
			svn.deleteFile(editor, "/snv/开发类/a/itemC1.txt", revisionNo);
//			System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
//		} catch (SVNException e) {
//			e.printStackTrace();
//		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020217.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号