Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @RequestParam无法';找不到CommonMultipartFile附件[]_Spring_Jsp_Spring Mvc_Spring Boot_Jakarta Mail - Fatal编程技术网

Spring @RequestParam无法';找不到CommonMultipartFile附件[]

Spring @RequestParam无法';找不到CommonMultipartFile附件[],spring,jsp,spring-mvc,spring-boot,jakarta-mail,Spring,Jsp,Spring Mvc,Spring Boot,Jakarta Mail,我正在使用Spring4创建一个电子邮件服务,用户可以上传多个附件,输入来自JSP页面。我已通过以下方式实施该服务: JSP页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html > <html> <head> <meta http-equiv="Content

我正在使用Spring4创建一个电子邮件服务,用户可以上传多个附件,输入来自JSP页面。我已通过以下方式实施该服务:

JSP页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Spring MVC - Email</title>
</head>
<body>
<h1>Spring MVC - Send e-mail with attachments</h1>

<form method="post" action="/sendEmail2" enctype="multipart/form-data">
    Email To:
    <input type="text" name="mailTo[]" size="65"/><br>

    CC:
    <input type="text" name="ccTo[]" size="65"/><br>

    Subject:
    <input type="text" name="subject" size="65"/><br>

    Message:
    <textarea cols="50" rows="10" name="message"></textarea><br>

    From:
    <input type="text" name="from" size="65"/><br>

    Attach file:
    <input type="file" name="attachFile[]" size="60"/><br>

    <input type="submit" value="Send E-mail"/>
</form>
</body>
</html>
电子邮件服务:

@Service
public class EmailService2 implements ReportingService2 {

    @Override
    public boolean sendEmail(String[] toEmailAddresses, String[] toCc,
                             String subject, String message,
                             CommonsMultipartFile[] attachFile,
                             String fromName, String userName,
                             String password, String hostName,
                             String encoding, String protocol,
                             String portNo, boolean TLS, boolean sendPartial,
                             boolean SMTPAuth, boolean SSL) {

        Properties mailProperties = new Properties();

        if (SSL) {
            // if SSL is used then port no should be 465
            mailProperties.put("mail.smtp.ssl.enable", true);
            mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            mailProperties.put("mail.smtp.socketFactory.port", portNo);
            mailProperties.put("mail.smtp.socketFactory.fallback", false);

        } else if (TLS) {
            //if TLS is used then port no should be 587
            mailProperties.put("mail.smtp.starttls.enable", true);
        }

        if (SMTPAuth) {
            mailProperties.put("mail.smtp.auth", true);
        }

        if (sendPartial) {
            mailProperties.put("mail.smtp.sendpartial", true);
        }

        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setJavaMailProperties(mailProperties);

        //if no TLS and no SSL then default port no is 25
        javaMailSenderImpl.setPort(Integer.parseInt(portNo));
        javaMailSenderImpl.setHost(hostName);
        javaMailSenderImpl.setUsername(userName);
        javaMailSenderImpl.setPassword(password);
        javaMailSenderImpl.setDefaultEncoding(encoding);

        javaMailSenderImpl.send(mimeMessage -> {

            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, encoding);

            messageHelper.setSubject(subject);
            messageHelper.setText(message);
            messageHelper.setFrom(fromName);

            if (toEmailAddresses != null) {
                for (String singleEmailTo : toEmailAddresses) {
                    messageHelper.setTo(singleEmailTo);
                }
            }

            if (toCc != null) {
                for (String singleCc : toCc) {
                    messageHelper.setCc(singleCc);
                }
            }

            if (attachFile != null) {
                for (CommonsMultipartFile singleFile : attachFile) {
                    messageHelper.addAttachment(singleFile.getOriginalFilename(), singleFile);
                }
            }
        });


        return true;
    }
}
现在,当我运行项目并点击映射时,我得到的错误在控制器中。以下是错误消息:

**HTTP Status 400 – Bad Request**

**Type** Status Report

**Message** Required CommonsMultipartFile[] parameter 'attachFile' is not present

**Description** The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

Apache Tomcat/8.5.23
我尝试以以下方式接收附件数组,但它显示不兼容类型:

CommonsMultipartFile[] files=request.getParameterValues("attachFile[]");
请帮助…

使用控制器

@RequestParam("attachFile") 

正如错误消息所说,spring框架正在寻找一个名为“attachFile”的参数。

RU如何命中此映射。使用诸如postman之类的rest客户机或使用其他代码?我通过JSP页面点击。我已经提供了上面的实现代码。在您的控制器用户
@RequestParam(“attachFile”)
中,我可以知道为什么要使用commonmultipartfile。您可以改用MultipartFile。另外,我认为在jsp中应该是这样的
name=“attachFile”
。我不知道。只是一个猜测,我对spring还不熟悉,所以我经历了很多次。不过,你的建议奏效了,效果很好。我以@RequestParam(“attachFile[])CommonMultipartFile attachFile[]的形式尝试了它。完成。干杯看一看。我觉得你应该改用MultipartFile。检查一下@RequestParam(“attachFile[]”)在我从html发送数组时得到的结果。
@RequestParam("attachFile")