springBoot使用multipart/form-data表单格式,接口传送文件格式的数据

@RequestMapping("/sent-mail")
public ReturnT<String> sentMail(@RequestParam(value = "recipients",required = false) String recipients,
                                @RequestParam(value = "subject",required = false) String subject,
                                @RequestParam(value = "sender",required = false) String sender,
                                @RequestParam(value = "files",required = false) MultipartFile[] files,
                                @RequestParam(value = "mailContext",required = false) String mailContext
) throws MessagingException, IllegalAccessException, IOException {
    ReturnT<String> result = ReturnT.SUCCESS;
    sendEmailUtil.sendEmail(recipients,subject,sender,files,mailContext);
    return result;
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class SendEmailUtil {
    @Autowired
    private RestTemplate restTemplate;
//单文件
    public JSONObject sendEmail(String url,String recipients, String subject, String sender, MultipartFile file, String mailContext) throws IOException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("recipients", recipients);
        map.add("subject", subject);
        map.add("sender", sender);
        map.add("sendSys", "dahua-b-itr");
        ArrayList<Object> fileList = new ArrayList<>();
        String filename = file.getOriginalFilename();
        String tempFilePath = System.getProperty("user.dir") + filename;
        File tempFile = new File(tempFilePath);
        file.transferTo(tempFile);
        FileSystemResource resource = new FileSystemResource(tempFilePath);
        map.add("attachments", resource);
        map.add("mailContext", mailContext);
        HttpEntity<LinkedMultiValueMap<String, Object>> param = new HttpEntity<>(map, httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url, param, String.class);
        tempFile.delete();
        return JSON.parseObject(response.getBody());
    }
//多文件
    public JSONObject sendEmail(String url,String recipients, String subject, String sender, MultipartFile[] files, String mailContext) throws IOException {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("recipients", recipients);
        map.add("subject", subject);
        map.add("sender", sender);
        map.add("sendSys", "dahua-b-itr");
        for (MultipartFile file : files) {
            ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
                @Override
                public String getFilename() {
                    return file.getOriginalFilename();
                }
            };
            map.add("attachments", resource);
        }
        map.add("mailContext", mailContext);
        HttpEntity<LinkedMultiValueMap<String, Object>> param = new HttpEntity<>(map, httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url, param, String.class);
//        tempFile.delete();
        return JSON.parseObject(response.getBody());
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值