gpt4 book ai didi

spring-mvc - org.springframework.web.multipart.MultipartException : The current request is not a multipart request

转载 作者:行者123 更新时间:2023-12-03 08:29:52 27 4
gpt4 key购买 nike

我正在尝试向服务器发送多部分请求,但出现以下异常
HTTP 状态 500 - 请求处理失败;嵌套异常是 org.springframework.web.multipart.MultipartException: 当前请求不是多部分请求

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<form id="imageuploadForm" action="<c:url value="/members/profileimageupload" />" method="POST" enctype="multipart/form-data">
<div style="width:40%; float:left">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"></div>
<div>
<span class="btn btn-file">
<span class="fileupload-new">Select image</span>
<span class="fileupload-exists">Change</span>
<input id="imageFile" name="imageFile" type="file" />
</span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
</div>
<div style="width:40%; float:right" >
<div class="progress">
<div class="bar" style="width: 60%;"></div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-success" id="submit">Upload</button>
<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true" >Close</button>
</div>
</div>

我的 ajax 调用正在发送请求。
$(function() {
//twitter bootstrap script
$("button#submit").click(function(){
var $form = $("#imageuploadForm");
var type = $form.attr('method');
var url = $form.attr('action');
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),

success: function(msg){
$("#form-content").modal('hide');
},

error: function(){
}
});
});
});

我的 Controller 应该处理请求
@RequestMapping(value="/profileimageupload",method= RequestMethod.POST)
public void uploadProfileImage(@RequestParam(value="imageFile") final MultipartFile file) throws NumberFormatException, IOException{
////
}

我有以下多部分文件的 Web 配置
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

/**
* Supports FileUploads.
*/
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(500000000);
return multipartResolver;
}
}

最佳答案

检查您是否在 Spring-Servlet.xml 中添加了 CommonsMultipartResolver。

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

然后,
将 enctype 添加到表单中的 multipart/form-data
<form id="fileupload" method="post" enctype="multipart/form-data">

最后在 Controller 中,
请求 > MultipartHttpServletRequest
 @RequestMapping(value = "/profileimageupload", method = RequestMethod.POST)
public ModelAndView uploadProfileImage(MultipartHttpServletRequest request) {}

依赖关系

  1. commons-fileupload.jar
  2. commons-io.jar

关于spring-mvc - org.springframework.web.multipart.MultipartException : The current request is not a multipart request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com