参数填写指南
请求参数(header):
参数填写指南| 参数名 | 必填 | 描述 | 默认值 |
|---|---|---|---|
| Content-Type | 是 | 请求体数据类型 | application/json |
| usercode | 是 | 用户 | |
| password | 是 | 密码 | |
| system | 是 | 系统参数 |
请求参数(body):
| 参数名 | 类型 | 是否数组 | 必填 | 描述 | 默认值 |
|---|---|---|---|---|---|
| path | 否 | 是 | 文件全路径 |
返回类型参数:
| 参数名 | 类型 | 是否数组 | 必填 | 描述 | 默认值 |
|---|---|---|---|---|---|
| data | BASE64 编码的字符串 |
代码示例:
请求参数示例:
复制代码
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import nc.vo.pub.BusinessException;
import net.sf.json.JSONObject;
public class TestAPI4JavaDemoCaller {
public static void main(String[] args) throws InterruptedException, IOException, BusinessException {
// 服务器访问地址及端口,例如 http://ip:port
String serviceUrl = "http://127.0.0.1:80";
// 服务名
String serviceName = "/u8cloud/api/file/download/base64";
// 各服务的参数设置,具体可参照服务描述页面的请求参数示例
String json = "";
// 使用U8cloud系统中设置,具体节点路径为:
// 应用集成 - 系统集成平台 - 系统信息设置
// 设置信息中具体属性的对照关系如下:
Map<String, Object> map = new HashMap<String, Object>();
map.put("trantype", ""); // 档案翻译方式,枚举值为:编码请录入 code, 名称请录入 name, 主键请录入 pk
map.put("system", "liuyxp01"); // 系统编码
map.put("usercode", "liuyxp01"); // 用户
map.put("needStackTrace", "Y");
map.put("password", "6c68086695e3282e8d688ae5889851b8"); // 密码,需要 MD5 加密后录入
try {
map.put("path", URLEncoder.encode("1012A210000000001KEO/hani.png", // 要下载的文档管理处路径
"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 返回结果
String result = operator(serviceUrl + serviceName, map, json);
JSONObject jsonObject = JSONObject.fromObject(result);
String fileStr = jsonObject.getString("data");
JSONObject fileObj = JSONObject.fromObject(fileStr);
String filereStr = fileObj.getString("base64file");
byte[] filebyte =Base64.getDecoder().decode(filereStr);
downLoadFile( filebyte);
System.out.println(result);
}
/**
*
* @Title: base64ToOutputStream
* @Description:
* @author
* @param result
* @return
* @throws IOException
* @throws BusinessException
*/
private static void downLoadFile( byte[] filebyte) throws IOException, BusinessException{
String downloadpath ="D:\\apitest\\"; // 文件夹位置
String filname = "下载后的文件(777).png";
File file=new File(downloadpath+filname);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
throw new BusinessException("文件太大");
}
outputStream.write(filebyte);
outputStream.flush();
outputStream.close();
}
private static String operator(String url, Map<String, Object> headparams,
String json) {
HttpClient httpClient = new HttpClient();
PostMethod httpPost = new PostMethod(url);
httpPost.setRequestHeader("content-type",
"application/json;charset=utf-8");
for (Entry<String, Object> entry : headparams.entrySet()) {
httpPost.setRequestHeader(entry.getKey(), entry.getValue()
.toString());
}
try {
RequestEntity entity = new StringRequestEntity(json,
"application/json", "UTF-8");
httpPost.setRequestEntity(entity);
httpClient.executeMethod(httpPost);
return httpPost.getResponseBodyAsString();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}正确返回示例:
复制代码
{
"status": "success",
"data": "BASE64 编码的字符串"
}错误返回示例:
复制代码
{
"status": "falied",
"errorcode": "-3200",
"errormsg": "U8C返回信息:文件不存在"
}