springboot实现多参数的同时上传多个文件

一.controller层

    @PostMapping("/mail/files")
    public BaseDataResponse sendFiles(@RequestParam("tos") String tos, @RequestParam("subject") String subject,
                                      @RequestParam("content") String content, @RequestParam("useHtml") boolean useHtml,
                                      @RequestParam("ccs") String ccs, @RequestParam("bccs") String bccs,
                                      @RequestPart("files") MultipartFile[] files) {
        if (StringUtils.isAnyBlank(tos, subject, content)) {
            throw new MailException(CommonRespEnum.ERROR_PARAM_EXCEPTION);
        }
        final File[] filesArray = MultipartFileUtil.convertMultipartToFiles(files);
        final MailSendRequest request = MailSendRequest.builder().tos(tos).subject(subject)
                .content(content).useHtml(useHtml).ccs(ccs)
                .bccs(bccs).files(filesArray).build();
        return smsSendService.sendFiles(request);
    }

1.注意MultipartFile[] files对象的接收,要使用注解@RequestPart(“files”) ,而不是 @RequestParam

2.MultipartFile转File的工具类

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

/**
 * MultipartFile和File互转工具类
 */
@Slf4j
public class MultipartFileUtil {

    /**
     * MultipartFileUtil 转File
     *
     * @param multipartFile
     * @return
     */
    public static File convertMultipartToFile(MultipartFile multipartFile) {
        String fileName = multipartFile.getOriginalFilename();
        File file = new File(fileName);
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            byte[] ss = multipartFile.getBytes();
            for (byte s : ss) {
                out.write(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error("{}转化File异常{]", fileName, e);
                }
            }
        }
        return file;
    }

    public static File[] convertMultipartToFiles(MultipartFile[] multipartFiles) {
        if (multipartFiles == null || multipartFiles.length == 0) {
            return null;
        }
        File[] fileArray = new File[multipartFiles.length];
        for (int i = 0; i < multipartFiles.length; i++) {
            File file = convertMultipartToFile(multipartFiles[i]);
            fileArray[i] = file;
        }
        return fileArray;
    }
}

    /**
     * MultipartFileUtil 转File
     * 该方法会有问题,生成的file对象,大小为空,不要使用
     * @param multipartFile
     * @return
     */
//    public static File convertMultipartToFile(MultipartFile multipartFile) {
//        final String originalFilename = multipartFile.getOriginalFilename();
//        System.out.println(multipartFile.getSize());
//        File file = new File(originalFilename);
//        boolean transferSuccess = false;
//        try {
//            if (!file.exists()) {
//                file.createNewFile();
//            }
//            multipartFile.transferTo(file);
//            transferSuccess = true;
//        } catch (IOException e) {
//            log.error("{}multipartFile转换异常:{}", originalFilename, e);
//        }
//        if (!transferSuccess) {
//            log.error("Failed to transfer multipartFile to file:{}", originalFilename);
//        }
//        System.out.println(file.length());
//        return file;
//    }

你可能感兴趣的:(java,spring,boot,spring)