您好,欢迎来到爱站旅游。
搜索
您的当前位置:首页springBoot使用multipart/form-data表单格式,接口传送文件格式的数据

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());
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- azee.cn 版权所有 赣ICP备2024042794号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务