【问题标题】:Could not parse multipart servlet request; nested exception is FileUploadException: the request was rejected because no multipart boundary was found无法解析多部分 servlet 请求;嵌套异常是 FileUploadException:请求被拒绝,因为没有找到多部分边界
【发布时间】:2015-12-17 05:05:05
【问题描述】:

在我当前的 spring-boot 项目中,我有这个表单可以将一个 war 文件上传到服务器:

    <form:war>
        <div class="field-box">
             <label th:text="${war}"></label>
             <div class="col-md-7">
                 <input class="form-control arquivo" th:attr="data-classe=${command['class'].simpleName},target=${war}" th:id="${war}" type="file" />
             </div>
         </div>
    </form:war>

表单由这个控制器方法处理:

  @RequestMapping(value="download", method=RequestMethod.GET)
    @ResponseBody
    public HttpEntity<byte[]> download(@RequestParam(value="id") String id) throws Exception {
        return this.serv.download(id);
    }

并通过服务类中的这个方法:

  public String upload(String id, String classe, String target, MultipartFile file) throws Exception {
    Arquivo novo;
    if(id == null) {
      novo = new Arquivo(classe, target);
      this.dao.insert(novo);
            id = novo.getId();
    } else {
      novo = this.dao.findById(id);
    }

    byte[] bytes = file.getBytes();
    File arquivo = new File(novo.toString());
    if(!arquivo.exists())
            if(arquivo.mkdirs())
                arquivo.createNewFile();

    BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(arquivo) );
    stream.write(bytes);
    stream.close();

    return novo.getId();
  }

这一切都是由这个 jquery 代码触发的:

        $("input.arquivo").on("change", function(event){
            console.log('mudanca no campo de arquivo');
            var c = $(this).data('classe');
            var t = $(this).data('target');
            var f = $(this).val();
            $.ajax({
                method: "POST",
                url: "/Picture/upload",
                data: { classe: c, target: t, file: f },
                contentType: "multipart/form-data"
            }).done(function(data){
                console.log('id: '+data);
                $(this).attr('type', 'hidden');
                $(this).attr('name', t);
                $(this).attr('value', data)
            });
        });

我的 application.properties 有这个配置:

# Multipart Configuration
multipart.location = ${user.home}/.lojacms/Uploads
multipart.maxFileSize = 100Mb
multipart.maxRequestSize = 100Mb
multipart.fileSizeThreshold = 10Mb

但是当我尝试上传文件时,我收到了上述错误(在标题中)。谁能告诉我这里出了什么问题?

【问题讨论】:

  • 你能添加你的控制器 RequestMapping 来上传吗?

标签: spring file-upload spring-boot multipartform-data multipart


【解决方案1】:

好的,然后我找到了将javascript代码更改为此的解决方案:

          $("input.arquivo").on("change", function(event){
                var c = $(this).data('classe');
                var t = $(this).data('target');

                var formData = new FormData();
                formData.append('classe', c);
                formData.append('target', t);
                formData.append('file', $('#input_'+t)[0].files[0]);

                $.ajax({
                  method: "POST",
                  url: "/Arquivo/upload",
                  data: formData,
                  cache: false,
                  contentType: false,
                  processData: false
                }).done(function(data){
                  $('#'+t).append('<input type="hidden" name="'+t+'" value="'+data+'"/>')
                });
          });

简而言之,我使用 javascript 的 FormData 元素来处理提交给服务器的值,包括多部分数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2020-11-21
    • 2013-07-02
    • 2019-03-18
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多