官网
SDK下载地址
https://cms.yozocloud.cn/info/file/getResource/81
DEMO下载地址
https://cms.yozocloud.cn/info/file/getResource/82
上传文件:上传本地文件至服务器,上传成功后会返回对应的fileID和fileVersionId,用户可以使用fileVersionId实现在线操作。
流程:前端选择文件 > java后台接收 > 调用永中提供的接口上传文件。
生成签名(注意点:添加接口使用到的参数以及对应的参数值到集合中):
private String getSign(Map<String, String[]> map) throws Exception {
map.put("appId", new String[]{config.getAppId()});
AppAuthenticator authenticator = new UaaAppAuthenticator(UaaConstant.SIGN, null, UaaConstant.APPID);
String sign = authenticator.generateSign(config.getAppKey(), map);
return sign;
}
上传文件完整版
@ResponseBody
@PostMapping(value = "upload")
public void getSessionToken(@RequestParam("file") MultipartFile multipartFile) throws Exception {
Map<String, String[]> params = new HashMap<>();
String sign = getSign(params);
String url = "http://dmc.yozocloud.cn/api/file/upload?appId={0}&sign={1}";
url = url.replace("{0}", config.getAppId()).replace("{1}", sign);
System.out.println("url:" + url);
//上传文件
//String filePath = "g:\\学院专业关系表.xlsx";
//设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);//文件类型是multipartFile类型
//HttpHeaders fileHeader= new HttpHeaders();
//fileHeader.setContentType(MediaType.parseMediaType(multipartFile.getContentType()));
//fileHeader.setContentDispositionFormData("file", multipartFile.getOriginalFilename());
//设置请求体,注意是LinkedMultiValueMap
//HttpEntity<ByteArrayResource> httpEntity = new HttpEntity<>(new ByteArrayResource(multipartFile.getBytes()), fileHeader);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
//form.add("file", httpEntity);
form.add("file", multipartFile.getResource());
//用HttpEntity封装整个请求报文
HttpEntity files = new HttpEntity<>(form, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, files, String.class);
System.out.println("返回信息为: " + response.getBody());
}