请问上传文件时,文件名需要做encodeURL(filename)处理吗?
上传临时素材中文文件名乱码?这是代码 @Test public void testUploadMedia() { FileSystemResource resource = new FileSystemResource("/Users/lanyanhua/Desktop/中文名.jpg"); String mediaId = uploadMedia(resource); System.out.println("mediaId:" + mediaId); //3xyN1by2qk0dVfEwKJ2ZvC4LA38qGprE3xMRbr5U6h2g //3XmJgRO7QE9mH_WCaYNgE4ETur61Few3MKTL79lzCYiWTa_Re2Q2CLX5OsCeYmAg- } /** * 上传临时素材 * * @param resource 文件 * @return media_id,该media_id仅三天内有效 */ public String uploadMedia(FileSystemResource resource) { String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE" .replace("ACCESS_TOKEN", token).replace("TYPE", "image"); RestTemplate restTemplate = new RestTemplate(); MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("file", resource); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(param, headers); ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, requestEntity, JSONObject.class); if (responseEntity.getStatusCode() == HttpStatus.OK) { JSONObject body = responseEntity.getBody(); if (body.getInteger("errcode") == 0) { return body.getString("media_id"); } } throw new BusinessException("上传临时素材失败"); } 获取时名字就是乱码的 [图片]
2020-11-20