依赖包
<dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.10.1</version> </dependency>
svn路径等配置类
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Getter
@Setter
@ConfigurationProperties("svn.resource")
public class SvnConnectProperties {
private String url;
private String remote;
private String name;
private String password;
}
操作svn工具类
import com.xxx.Exception.HZException;
import com.xxx.enums.HttpStatus;
import com.xxx.ExceptionUtils;
import com.xxx.config.SvnConnectProperties;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import java.io.*;
import java.util.Collection;
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(SvnConnectProperties.class)
public class SvnHandler {
private final SvnConnectProperties connectProperties;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public SVNRepository getRepository(String url, String name, String password) {
SVNRepository repository = null;
try {
repository = DAVRepositoryFactory.create(SVNURL.parseURIEncoded(url));
ISVNAuthenticationManager authManager = new BasicAuthenticationManager(name, password);
repository.setAuthenticationManager(authManager);
} catch (SVNException e) {
ExceptionUtils.handlerException(e, logger);
}
return repository;
}
public SVNRepository getRepository() {
return getRepository(connectProperties);
}
public SVNRepository getRepository(SvnConnectProperties config) {
SVNRepository repository = getRepository(config.getUrl(), config.getName(), config.getPassword());
SVNNodeKind kind = checkPath(repository, "");
return (SVNNodeKind.UNKNOWN == kind || SVNNodeKind.NONE == kind) ?
getRepository(config.getRemote(), config.getName(), config.getPassword()) : repository;
}
public String getActiveUrl() {
SVNRepository repository = getRepository(connectProperties.getUrl(), connectProperties.getName(), connectProperties.getPassword());
SVNNodeKind kind = checkPath(repository, "");
return (SVNNodeKind.UNKNOWN == kind || SVNNodeKind.NONE == kind) ? connectProperties.getRemote() : connectProperties.getUrl();
}
public SVNUpdateClient getSvnUpdataClient() {
setupDavLibrary();
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager manager = SVNClientManager.newInstance((DefaultSVNOptions) options, connectProperties.getName(), connectProperties.getPassword());
SVNUpdateClient client = manager.getUpdateClient();
client.setIgnoreExternals(false);
return client;
}
public Collection<SVNDirEntry> listEntries(String path) {
setupDavLibrary();
return listEntries(getRepository(), path);
}
public SVNNodeKind checkPath(SVNRepository repository, String path) {
SVNNodeKind result = SVNNodeKind.UNKNOWN;
try {
result = repository.checkPath(path, -1);
} catch (SVNException ignored) {
}
return result;
}
public SVNNodeKind checkPath(String path) {
setupDavLibrary();
return checkPath(getRepository(), path);
}
private Collection<SVNDirEntry> listEntries(SVNRepository repository, String path) {
Collection<SVNDirEntry> result = null;
try {
result = repository.getDir(path, -1, null, (Collection) null);
} catch (SVNException e) {
ExceptionUtils.handlerException(e, logger);
}
return result;
}
public void setupDavLibrary() {
DAVRepositoryFactory.setup();
}
public SvnConnectProperties getConnectProperties() {
return connectProperties;
}
public void getOutputStream(SVNRepository repository, String path, SVNProperties properties, OutputStream outputStream) {
try {
SVNNodeKind nodeKind = repository.checkPath(path, -1);
if (nodeKind == SVNNodeKind.NONE) {
throw new HZException(HttpStatus.error.getCode(), path + "不存在");
} else if (nodeKind != SVNNodeKind.FILE) {
throw new HZException(HttpStatus.error.getCode(), path + "不是文件");
}
repository.getFile(path, -1, properties, outputStream);
} catch (SVNException e) {
ExceptionUtils.handlerException(e, logger);
}
}
public boolean downloadFile(File file, String path) {
SVNProperties properties = new SVNProperties();
OutputStream outputStream = null;
try {
if (!file.exists()) {
file.createNewFile();
}
outputStream = new FileOutputStream(file);
setupDavLibrary();
getOutputStream(getRepository(), path, properties, outputStream);
} catch (IOException e) {
ExceptionUtils.handlerException(e, logger);
} finally {
if (null != outputStream) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException ignored) {
}
}
}
return true;
}
}