赞
踩
记录一下,发送请求时,替换文件名的一种写法。
下面这个方法是 上传文件到企微。
- public String uploadMedia(String key,String type,String filePath,String fileName) {
- byte[] fileToByte = FileUtil.getFileToByte(filePath);
- ByteArrayResource resource = new ByteArrayResource(fileToByte){
- @Override
- public String getFilename() {
- return fileName;
- }
- };
- String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=%s&type=%s",key,type);
- MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
- formData.add("file",resource);
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.MULTIPART_FORM_DATA);
- HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(formData, headers);
- ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
- return response.getBody();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。