本文共 8723 字,大约阅读时间需要 29 分钟。
作者:zhangL
iserver rest api提供了一系列针对iserver的接口,在帮助文档中都可以查到;文件上传和发布服务不可或缺的一步就是用户验证。本文是用的token令牌进行认证,对 tokens 资源执行 POST 请求:http://localhost:8090/iserver/services/security/tokens.rjson
//获取token 令牌发放方式为IP验证 public static String geToken(String user,String pwd,String ip,String url){ String token=null; String jsonString="{\"userName\":\""+user+"\",\"password\":\""+pwd+"\",\"clientType\":\"RequestIP\",\"ip\":\""+ip+"\",\"expiration\":20}";// JSONObject jsonObject=JSONObject.parseObject(jsonString); HttpPost post = null; HttpEntity entityRe = null; BufferedReader reader = null; String line = null; String resultString=""; try { HttpClient httpClient = new DefaultHttpClient(); // 设置超时时间 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); post = new HttpPost(url); // 构造消息头 post.setHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Connection", "Close"); post.addHeader("Accept","*/*"); // 构建消息实体 StringEntity entity = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")); entity.setContentEncoding("UTF-8"); // 发送Json格式的数据请求 entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = httpClient.execute(post); entityRe = response.getEntity(); reader = new BufferedReader(new InputStreamReader(entityRe.getContent(), "UTF-8")); while ((line = reader.readLine()) != null) { resultString += line; } token = resultString; System.out.println("token创建状态码:"+response.getStatusLine().getStatusCode());//输出返回状态码 } catch (Exception e) { // TODO: handle exception } return token; }
对 uploadTasks 资源http://localhost:8090/iserver/manager/filemanager/uploadtasks.rjson,执行 POST 请求,返回一个uploadTask
//创建上传任务 public static String getTask(String path,String url,String token) { String taskId=null; String jsonString="{\"path\":\""+path+"\"}";//,\"fileSize\":1745000,\"md5\":\"65b49e3f8c243f762bf0360ddc8e55c9\" JSONObject jsonObject=JSONObject.parseObject(jsonString); HttpPost post = null; HttpEntity entityRe = null; BufferedReader reader = null; String line = null; String resultString=""; try { HttpClient httpClient = new DefaultHttpClient(); // 设置超时时间 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); post = new HttpPost(url+"?token="+token); // 构造消息头 post.setHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Connection", "Close"); post.addHeader("Accept","*/*"); // 构建消息实体 StringEntity entity = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")); entity.setContentEncoding("UTF-8"); // 发送Json格式的数据请求 entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = httpClient.execute(post); entityRe = response.getEntity(); reader = new BufferedReader(new InputStreamReader(entityRe.getContent(), "UTF-8")); while ((line = reader.readLine()) != null) { resultString += line+"\n"; } taskId = resultString; System.out.println("任务创建状态码:"+response.getStatusLine().getStatusCode());//输出返回状态码 } catch (Exception e) { // TODO: handle exception } //System.out.println(taskId); JSONObject idjson = JSONObject.parseObject(taskId); taskId = idjson.getString("newResourceID"); return taskId; }
对上传文件资源 uploadTask 执行 POST 请求执行上传
//执行上传任务 public static String startUpFile(String topath,String url,String token) { File file = new File("D:/idesktopdata/testzoom.smwu"); // 创建文件对象(待上传文件) AbstractContentBody srcFileBody = new FileBody(file); String result=null; HttpPost post = null; HttpEntity entityRe = null; BufferedReader reader = null; String line = null; String resultString=""; try { HttpClient httpClient = new DefaultHttpClient(); // 设置超时时间 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); post = new HttpPost(url+"?&toFile="+topath+"&overwrite=true&token="+token); System.out.println(url+"?token="+token); // 构造消息头:::文件上传header的Content-type最好不要设,用默认的 // post.setHeader("Content-type", "multipart/form-data; boundary=; charset=utf-8"); post.setHeader("Connection", "Close"); post.addHeader("Accept","*/*"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("utf-8")); entity.addPart("file", srcFileBody); post.setEntity(entity); HttpResponse response = httpClient.execute(post); System.out.println(post.getEntity().getContentType().toString()); entityRe = response.getEntity(); reader = new BufferedReader(new InputStreamReader(entityRe.getContent(), "UTF-8")); while ((line = reader.readLine()) != null) { resultString += line+"\n"; } result = resultString; System.out.println("上传状态码:"+response.getStatusLine().getStatusCode());//输出返回状态码 } catch (Exception e) { // TODO: handle exception } return result; }
到此文件上传已经结束!
对 workspaces 资源:http://localhost:8090/iserver/manager/workspaces.rjson 发送 POST 请求,将工作空间 World.sxwu 快速发布成 RESTMAP 服务,也需要token令牌认证
//发布工作空间服务 public static void upService(String workspaceConnectionInfo,String url,String token) { String jsonString="{\"servicesTypes\": [\"RESTMAP\",\"RESTDATA\"] ,\"workspaceConnectionInfo\":\""+workspaceConnectionInfo+"\"}";// System.out.println(jsonString); JSONObject jsonObject=JSONObject.parseObject(jsonString); HttpPost post = null; HttpEntity entityRe = null; BufferedReader reader = null; String line = null; String resultString=""; try { HttpClient httpClient = new DefaultHttpClient(); // 设置超时时间 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000); post = new HttpPost(url+"?token="+token); // 构造消息头 post.setHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Connection", "Close"); post.addHeader("Accept","*/*"); // 构建消息实体 StringEntity entity = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")); System.out.println(jsonObject.toString()); entity.setContentEncoding("UTF-8"); // 发送Json格式的数据请求 entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = httpClient.execute(post); entityRe = response.getEntity(); reader = new BufferedReader(new InputStreamReader(entityRe.getContent(), "UTF-8")); while ((line = reader.readLine()) != null) { resultString += line; } System.out.println("服务发布状态码:"+response.getStatusLine().getStatusCode());//输出返回状态码 } catch (Exception e) { // TODO: handle exception } }
数据库型的工作空间同理,只需要按照rest api,workspaceConnectionInfo文件型指的是工作空间路径,数据库型则需要对应的连接参数,比如:“workspaceConnectionInfo”: “server=orcl203;username=test;password=test;type=ORACLE;database=;name=testWorkSpace;driver=null”
该demo为了方便下载和管理依赖,建的maven项目。所用到依赖参考:
junit junit 3.8.1 test org.apache.httpcomponents httpclient 4.4.1 org.apache.httpcomponents httpcore 4.4.1 org.apache.httpcomponents httpmime 4.4.1 net.sf.json-lib json-lib 2.4 jdk15 com.alibaba fastjson 1.2.51
转载地址:http://zspxi.baihongyu.com/