class="java" name="code">/** * 将本地文件上传到文件系统 * * @param region 用户所在数据中心 * @param userId 用户id * @param targetFolderUuid 目标文件夹的id * @param filePath 本地文件路径 * @return */ public static String uploadFileToFileSystem(String region, String userId, String targetFolderUuid, String filePath) { FileSystemResource resource = new FileSystemResource(filePath); if (!resource.exists() || Strings.isNullOrEmpty(targetFolderUuid)) { return null; } MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("file", resource); param.add("userId", userId); param.add("targetFolderUuid", targetFolderUuid); try { String fileServerIp = getFileServerIp(region); ResponseEntity responseEntity = innerTokenAccess(HttpMethod.POST, fileServerIp + UPLOAD_URL, param, MediaType.MULTIPART_FORM_DATA); JSONObject jsonObject = JSONObject.fromObject(responseEntity.getBody()); return jsonObject.getString("fileUuid"); } catch (Exception e) { LOG.info("上传文件到文件系统失败:{}", e.getMessage()); } return null; } /** * 使用RestTemplate访问远程接口 * * @param method 方法的请求类型 POST or GET OR ... * @param url 请求的url * @param obj 请求的参数 * @param mediaType 头信息类型 * @return * @throws Exception */ public static ResponseEntity innerTokenAccess(HttpMethod method, String url, Object obj, MediaType mediaType) throws Exception { LOG.info("访问的链接:" + url); RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(mediaType); HttpEntity httpEntity = new HttpEntity(obj, httpHeaders); ResponseEntity result = restTemplate.exchange(url, method, httpEntity, String.class); LOG.info("访问接口:" + url + "返回的值为:" + result.getStatusCodeValue()); return result; }
?