String str, String pattern, String replace) {
int strLen = str.length();
int patternLen = pattern.length(); int start = 0, end = 0;
StringBuffer result = new StringBuffer(strLen); char[] chars = new char[strLen];
while ( (end = str.indexOf(pattern, start)) >= 0) { str.getChars(start, end, chars, 0);
result.append(chars, 0, end - start).append(replace); start = end + patternLen; }
str.getChars(start, strLen, chars, 0); result.append(chars, 0, strLen - start);
return result.toString(); }
/*
* The main entry point for the application. */
public static void main(String[] args) {
// Change this URL if your server is in another location or a // different port. //服务器地址
String serverHost = \ //服务器端口号
String serverPort = \
// Change this search path if you want to run a different report. //报表的搜索路径 String reportPath =
\ + \Samples']/\
+ \
// Output filename 生成文件输出的物理路径名 String outputPath = \
// Specify a user to log in as. 登陆的用户名密码和名称空间 String userName = \ String userPassword = \
String userNamespace = \
// Parse the command-line arguments. 命令行参数 for (int idx = 0; idx < args.length; idx++) {
if (args[idx].compareTo(\ {
serverHost = args[idx + 1]; }
else if (args[idx].compareTo(\ {
serverPort = args[idx + 1]; }
else if (args[idx].compareTo(\ {
reportPath = args[idx + 1]; }
else if (args[idx].compareTo(\ {
outputPath = args[idx + 1]; }
else if (args[idx].compareTo(\ {
userName = args[idx + 1]; }
else if (args[idx].compareTo(\ {
userPassword = args[idx + 1]; }
else if (args[idx].compareTo(\ {
userNamespace = args[idx + 1]; } else {
System.out.println(\args[idx]);
System.exit(0); }
idx++;
}
// Create a connection to a report server. 开始创建连接 //服务器地址
String Cognos_URL = \
+ serverHost + \
+ serverPort
+ \
System.out.println(
\ + serverHost + \
+ serverPort + \
System.out.println(\ //创建reportServiceLocator
ReportService_ServiceLocator reportServiceLocator = new ReportService_ServiceLocator(); //创建报表服务
ReportService_Port repService = null;
try { //得到报表服务
repService =
reportServiceLocator.getreportService(new URL(Cognos_URL)); }
catch (MalformedURLException ex) {
System.out.println(
\ex.getMessage());
System.out.println(\Cognos_URL);
System.exit(-1); }
catch (ServiceException ex) {
System.out.println(\ex.getMessage());
ex.printStackTrace(); System.exit(-1); }
System.out.println(\
// Set up the biBusHeader for a logon. 用biBusHeader登陆 if ((userName.length() > 0)
&& (userPassword.length() > 0) && (userNamespace.length() > 0)) {
System.out.println(
\
+ userName + \
+ userNamespace + \
setUpHeader(repService, userName, userPassword, userNamespace); }
// Set up the report parameters. 设置报表参数
ParameterValue parameters[] = new ParameterValue[] {};
//为option数组 Option runOptions[] = new Option[5];
//设置是否保存报表
RunOptionBoolean saveOutput = new RunOptionBoolean(); saveOutput.setName(RunOptionEnum.saveOutput); saveOutput.setValue(false); runOptions[0] = saveOutput;
//设置报表运行格式
RunOptionStringArray outputFormat = new RunOptionStringArray(); outputFormat.setName(RunOptionEnum.outputFormat);
//格式还可以为CSV , HTMLFragment, MHT, PDF, singleXLS, XHTML, XLS, XLWA, XML
outputFormat.setValue(new String[] { \runOptions[1] = outputFormat;
RunOptionOutputEncapsulation outputEncapsulation =
new RunOptionOutputEncapsulation();
outputEncapsulation.setName(RunOptionEnum.outputEncapsulation);
outputEncapsulation.setValue(OutputEncapsulationEnum.none); runOptions[2] = outputEncapsulation;
//设置主要等待时间,单位秒
AsynchOptionInt primaryWait = new AsynchOptionInt();
primaryWait.setName(AsynchOptionEnum.primaryWaitThreshold); primaryWait.setValue(0); runOptions[3] = primaryWait;
//次要等待时间,单位秒
AsynchOptionInt secondaryWait = new AsynchOptionInt();
secondaryWait.setName(AsynchOptionEnum.secondaryWaitThreshold); secondaryWait.setValue(0); runOptions[4] = secondaryWait;
// Now, run the report. 开始运行报表 try {
System.out.println(\ System.out.println(\ System.out.println(reportPath); //设置超时时间
//java.net.SocketTimeoutException: Read timed out //((Stub)repService).setTimeout(0); //运行报表
AsynchReply res =
repService.run(new SearchPathSingleObject(reportPath), parameters, runOptions);
System.out.println(\
// The report is finished, let's fetch the results and save them to
// a file. 报表运行完毕,开始写文件生成到本地 if (res.getStatus() == AsynchReplyStatusEnum.complete)
{
AsynchDetailReportOutput reportOutput = null;