perforce java api使用

[quote]perforce是种版本管理软件,提供啦完整的java api,可以方便我们做一些客户化开发,官方文档http://www.perforce.com/product/components/apis[/quote]

1 . pom 依赖
<dependency>
<groupId>com.perforce</groupId>
<artifactId>p4java</artifactId>
<version>2011.1.297684</version>
</dependency>


2. 下载文件示例
public class PerforceUtil {
private static Logger logger = Logger.getLogger(PerforceUtil.class);

public static void SaveFile(InputStream inputStream, File file) {
try {
FileOutputStream out = new FileOutputStream(file);

byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1) {
out.write(b, 0, len);
}
inputStream.close();
out.close();

} catch (IOException e) {
logger.error("", e);
}
}

public static void downloadFile(String filePerforcePath, String destFolder) {
try {
IServer server = ServerFactory.getServer(ResourceUtil.getConfig().getProperty(Constants.P4PROTOCAL), null);
server.connect();
try {
server.setUserName(ResourceUtil.getConfig().getProperty(Constants.P4USERNAME));
server.login(ResourceUtil.getConfig().getProperty(Constants.P4PASSWORD));
IClient client = server.getClient(ResourceUtil.getConfig().getProperty(Constants.P4WORKSPACE));
if (client != null) {
server.setCurrentClient(client);
IServerInfo info = server.getServerInfo();
logger.info("server address: " + info.getServerAddress() + "\n" + "server version" + info.getServerVersion() + "\n"
+ "client address: " + info.getClientAddress() + "\n" + "client working directory: " + info.getClientCurrentDirectory()
+ "\n" + "client name: " + info.getClientName() + "\n" + "user name: " + info.getUserName());
List<IFileSpec> list =
client.sync(FileSpecBuilder.getValidFileSpecs(FileSpecBuilder.makeFileSpecList(new String[] {filePerforcePath})), true, false, false, false);


logger.info("download file size: " + list.size());


for (IFileSpec iFileSpec : list) {
String pathVersionInfo = iFileSpec.toString();
logger.info(iFileSpec);


File file = new File(iFileSpec.toString());
SaveFile(iFileSpec.getContents(true), file);
}
}
} catch (Exception exc) {
logger.error("", exc);
}

if (server != null) {
server.disconnect();
}
} catch (ConnectionException e) {
logger.error("", e);
} catch (NoSuchObjectException e) {
logger.error("", e);
} catch (ConfigException e) {
logger.error("", e);
} catch (ResourceException e) {
logger.error("", e);
} catch (AccessException e) {
logger.error("", e);
} catch (RequestException e) {
logger.error("", e);
} catch (URISyntaxException e) {
logger.error("", e);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值