API中心

  1. 通用服务
  2. 单据文档管理
返回

下载附件_Base64

接口所在U8C版本:U8 cloud3.5以及更高版本
详细描述:用于下载附件Base64
API视频讲解API详情补丁列表常见问题参考
接口地址
请求方式
请求参数header
请求参数body
返回类型参数
代码示例
请求参数示例
正确返回示例
错误返回示例
接口地址:http://ip:port/u8cloud/api/file/download/base64

参数填写指南

请求方式:POST

请求参数(header):

参数填写指南
参数名必填描述默认值
Content-Type请求体数据类型application/json
usercode用户
password密码
system系统参数

请求参数(body):

参数名类型是否数组必填描述默认值
path文件全路径

返回类型参数:

参数名类型是否数组必填描述默认值
dataBASE64 编码的字符串

请求参数示例:

代码示例

复制代码

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返回信息:文件不存在"
}