我正在使用Spring 4创建一个电子邮件服务,用户可以上传多个附件,输入来自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>
控制器:
@Controller
public class myController2 {
@Autowired
ReportingService2 service2;
@RequestMapping(value = "/sendEmail2", method = RequestMethod.POST)
public String sendEmail2(HttpServletRequest request,
@RequestParam CommonsMultipartFile attachFile[]
) {
String emailTo[] = request.getParameterValues("mailTo[]");
String ccTo[] = request.getParameterValues("ccTo[]");
String subject = request.getParameter("subject");
String msg = request.getParameter("message");
fromName = request.getParameter("from");
boolean res = service2.sendEmail(
emailTo, ccTo, subject, msg, attachFile,
fromName, username, password, hostName,
encoding, protocol, portNo, TLS,
sendPartial, SMTPAuth, SSL
);
if (res) {
return "/WEB-INF/Result";
} else {
return "errorpage";
}
}
电子邮件服务:
@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[]");
请帮忙......
答案 0 :(得分:1)
在您的控制器中使用
@RequestParam("attachFile")
正如错误消息所示,spring框架正在寻找名为'attachFile'的参数