资源简介

博客:使用iServer REST API发布工作空间(Java) (http://blog.csdn.net/supermapsupport/article/details/73201166) 的示例代码

资源截图

代码片段和文件信息

package com.supermap.MR;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class PublishWS
{

public static void main( String[ ] args )
{
// TODO Auto-generated method stub
PublishWS ps = new PublishWS( );
String result = ps.publish( “http://localhost:2811“ “supermap“ “supermap123!“
“F:/Data/TestData/GetCaption/GetCaption.smwu“ );
System.out.println( “############“ + result + “#########“ );
}

public String publish( String ipport String username String passwor String workspacePath )
{

String cookie = login( ipport username passwor );
System.out.println( “----------------“ + cookie + “---------------“ );
if ( cookie == ““ ) return null;

try
{

String json = “{\“servicesTypes\“: [\“RESTMAP\“ \“RESTDATA\“]\“workspaceConnectionInfo\“: \““
+ workspacePath + “\“\“isDataEditable\“:\“true\“}“;
System.out.println( “*****************“ + json + “*********************“ );
URL url = new URL( ipport + “/iserver/manager/workspaces.json“ );
HttpURLConnection connection = (HttpURLConnection) url.openConnection( );
connection.setDoInput( true );
connection.setDoOutput( true );
connection.setRequestMethod( “POST“ );
connection.setUseCaches( false );
connection.setInstanceFollowRedirects( true );
connection.setRequestProperty( “Content-Type“
“application/application/x-www-form-urlencoded; charset=UTF-8“ );
connection.setRequestProperty( “cookie“ cookie );
connection.connect( );
DataOutputStream out = new DataOutputStream( connection.getOutputStream( ) );

out.writeBytes( json );
out.flush( );
out.close( );

BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream( ) ) );
String lines;
StringBuffer sbf = new StringBuffer( );
while ( ( lines = reader.readLine( ) ) != null )
{
lines = new String( lines.getBytes( ) “utf-8“ );
sbf.append( lines );
}

reader.close( );
connection.disconnect( );

return sbf.toString( );

} catch ( MalformedURLException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
} catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace( );
}

return null;
}

/**
 * 登陆iServer
 * 
 * @param ipport
 *            iServer服务根目录,比如:“http://localhost:8090“
 * @param username
 *            登陆iServer的用户名
 * @param passwor
 *            登陆iServer的密码
 * @return String cookie
 */
private String login( String ipport String username String passwor )
{
try
{

String json = “{\“username\“: \““ + username + “\“\“password\“: \““ + passwor
+ “\“\“rememberme\“: \“true\“}“;

URL url = new URL( ipport + “/iserver/services/security/login.json

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        4296  2017-05-03 11:24  PublishWS.java

评论

共有 条评论