使用java spring发送电子邮件附件 - 多部分文件的错误400

时间:2017-09-07 14:03:56

标签: java angularjs spring maven spring-mvc

我想通过angular的ng-file-upload和java spring(使用multipart文件)通过电子邮件发送我的文件。

我有错误400:

  

无法构造org.springframework.web.multipart.MultipartFile的实例:抽象类型需要映射到具体类型

我的html按钮:

  <button type="button" ngf-select="uploadFiles($file)" ng-model="file" ngf-max-size="1MB" enctype="multipart/form-data">Select File</button>

我的角度控制器:

$scope.uploadFiles = function(file) {
    $scope.file = file;
}   

$scope.emailData.file = $scope.file;
    $http.post("sendemail", $scope.emailData)
        .then(function (response) {         
            $scope.succes =  true;
        },
        function(fail) {
            $scope.error = true;
        });

我发送的文件包含其他数据,如标题,说明等。

我用getter / setter声明了Multipart文件。

然后我的Java控制器:

@RequestMapping("/sendemail")

公共类EmailSenderController {

@Autowired
EmailSender emailSender;

@RequestMapping(method = RequestMethod.POST)
public EmailStatus sendEmail(@RequestBody EmailData emailData) {
    return emailSender.sendData(emailData);
}}

最后我有了EmailSender java类。

     private EmailStatus sendM(EmailData emailData){
        MimeMessage mail = javaMailSender.createMimeMessage();
         MimeMessageHelper helper = new MimeMessageHelper(mail, true);
         helper.setTo(to);
       // ANOTHER STUFF
         byte[] bytes;

      if(!emailData.getFile().isEmpty()) {
        bytes = emailData.getFile().getBytes();
        helper.addAttachment(emailData.getFile().getOriginalFilename(), new ByteArrayResource(bytes));
      }}

所以,我认为这一切都很好。我的pom.xml有依赖性:

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
</dependency>

我使用了这个例子:https://github.com/danialfarid/ng-file-upload/wiki/spring-mvc-example

0 个答案:

没有答案