首页 > 其他分享 >RestTemplate请求参数有MultipartFile[] 附件数组

RestTemplate请求参数有MultipartFile[] 附件数组

时间:2022-10-19 16:36:35浏览次数:87  
标签:false String RequestParam RestTemplate value add params 附件 MultipartFile

一、RestTemplate请求参数有MultiplateFile[]附件数组的情况,该如何处理

二、代码示例

@PostMapping("/testSendEmail")
    @ResponseBody
    public boolean testSendEmail(
            @RequestParam(value = "reToEmail", required = true) String reToEmail,
            @RequestParam(value = "reCcEmail", required = false) String reCcEmail,
            @RequestParam(value = "reBccEmail", required = false) String reBccEmail,
            @RequestParam(value = "subject", required = true) String subject,
            @RequestParam(value = "text", required = false) String text,
            @RequestParam(value = "content", required = false) String content,
            @RequestParam(value = "isSSL", required = false, defaultValue = "1") Integer isSSL,
            @RequestParam(value = "isUrgent", required = false, defaultValue = "false") Boolean isUrgent,
            @RequestParam(value = "files", required = false) MultipartFile[] files) {
        return emailService.testSendEmail(reToEmail, reCcEmail, reBccEmail, subject, text, content, isSSL, isUrgent, files);
    }
Boolean testSendEmail(String reToEmail, String reCcEmail, String reBccEmail, String subject, String text, String content, Integer isSSL,
                          Boolean isUrgent, MultipartFile[] files);
@Override
    public Boolean testSendEmail(String reToEmail, String reCcEmail, String reBccEmail, String subject, String text, String content, Integer isSSL,
                                 Boolean isUrgent, MultipartFile[] files) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("addEmail", addEmail);
        params.add("addName", addName);
        params.add("addPwd", addPwd);
        params.add("addServer", addServer);
        params.add("reToEmail", reToEmail);
        params.add("reCcEmail", reCcEmail);
        params.add("reBccEmail", reBccEmail);
        params.add("subject", subject);
        params.add("text", text);
        params.add("content", content);
        params.add("isSSL", isSSL);
        params.add("isUrgent", isUrgent);
        try {
            for (MultipartFile file : files) {
                String originalFilename = file.getOriginalFilename();
                System.out.println("----");
                System.out.println(originalFilename);
                ByteArrayResource byteArrayResource = new ByteArrayResource(file.getBytes()) {
		// 必须重写这两个方法,否则附件上传将失败
                    @Override
                    public long contentLength() {
                        return file.getSize();
                    }

                    @Override
                    public String getFilename() {
                        return file.getOriginalFilename();
                    }
                };
                params.add("file", byteArrayResource);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        org.springframework.http.HttpEntity<MultiValueMap<String, Object>> entity = new org.springframework.http.HttpEntity<>(params, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(sendEmailUrl, entity, String.class);
        System.out.println(responseEntity);
        if (responseEntity.getStatusCodeValue() == 200) {
            return true;
        }
        return false;
    }

postman示例
image

注:
1. new ByteArrayResource()里面必须重写contentLength()和getFilename()这两个方法。负责附件上传失败。
2. 需要重写getFilename、contentLength的原因:上传文件时RestTemplate会通过相应方法得到文件流的大小、文件名

参考文章:

  1. https://www.cnblogs.com/south-pigeon/p/13688465.html
  2. https://blog.csdn.net/qq123456789_/article/details/126339887

标签:false,String,RequestParam,RestTemplate,value,add,params,附件,MultipartFile
From: https://www.cnblogs.com/zlonger/p/16806747.html

相关文章