问题描述
所以我是Spring的新手,我试图让我的项目文件上传工作(我使用的是Spring工具套件),当提交表单时,我得到的是:来自浏览器的堆栈跟踪:
类型异常报告
消息无法解析多部分的servlet请求;嵌套的异常是java.lang.IllegalStateException:无法处理部分,因为没有提供多部分配置
描述服务器遇到内部错误,无法完成此请求。
异常
org.springframework.web.multipart.MultipartException:无法解析多部分的servlet请求;嵌套异常是java.lang.IllegalStateException:无法处理部分,因为没有提供多部分配置
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:100)
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest。(StandardMultipartHttpServletRequest.java:78)
org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:75)
org.springframework .web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:108)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
根本原因
java.lang.IllegalStateException:无法处理部分,因为没有提供多部分配置
org.apache.catalina.connector.Request.parseParts(Request.java:2676)
org。 apache.catalina.co nnector.Request.getParts(Request.java:2643)
org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1083)
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest。 parseRequest(StandardMultipartHttpServletRequest.java:85)
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest。(StandardMultipartHttpServletRequest.java:78)
org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver .java:75)
org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:108)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106 )
注意根源的完整堆栈跟踪在Apache Tomcat / 8.0.27日志中可用。
这是jsp中的表单标记:
< form:form class =form-horizontalrole =formmethod =post
action =newArtist.htmlmodelAttribute =artistenctype =multipart / form-data >
输入部分:
< div class =form-group>
< div class =col-lg-3>
< label for =photo>歌手照片:< / label>
< form:input type =fileid =photopath =photo>< / form:input>
< / div>
< / div>
照片存储在Artist对象的此字段中:
@Lob
private byte [] photo;
控制器映射方法:
@RequestMapping(value =/ newArtist,method = RequestMethod.GET)
public String showAddArtistForm(Model model)
{
model.addAttribute(artist ,新艺术家());
返回newArtist;
$ b @RequestMapping(value =/ newArtist,method = RequestMethod.POST)
public String addArtist(@ModelAttribute(artist)@Valid艺术家,BindingResult结果,
@RequestParam(photo)MultipartFile photo)抛出IOException
{
if(result.hasErrors())
returnnewArtist;
if(photo.getBytes()!= null)
artist.setPhoto(photo.getBytes());
artistService.addArtist(artist);
返回redirect:artists.html;
servlet-context.xml中的多重解析器配置:
< bean id =multipartResolverclass =org.springframework.web.multipart.commons.CommonsMultipartResolver>
< property name =maxUploadSizevalue =10000000/>
< / bean>
web.xml中的过滤器:
<滤光器>
< filter-name> MultipartFilter< / filter-name>
< filter-class> org.springframework.web.multipart.support.MultipartFilter< / filter-class>
< / filter>
< filter>
< filter-name> springSecurityFilterChain< / filter-name>
< filter-class> org.springframework.web.filter.DelegatingFilterProxy< / filter-class>
< / filter>
< filter-mapping>
< filter-name> MultipartFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
< filter-mapping>
< filter-name> springSecurityFilterChain< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
相依性:
<! - Apache Commons FileUpload - >
<依赖>
< groupId> commons-fileupload< / groupId>
< artifactId> commons-fileupload< / artifactId>
< version> 1.3.1< / version>
< /依赖>
<依赖>
< groupId> commons-io< / groupId>
< artifactId> commons-io< / artifactId>
< version> 2.4< / version>
< /依赖>
我还将Tomcat的配置文件context.xml导入到META-INF / context.xml中,并编辑了上下文标记像这样:
< Context allowCasualMultipartParsing =true>
没有任何东西可以工作,任何帮助都将不胜感激。
实际上,为了使用Spring MVC上传你的多部分文件,你不需要在web.xml中使用任何过滤器。我在我的项目中有相同的配置,它的工作($ {spring.version} = 4.3.4.RELEASE):
POM
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> $ {spring.version}< / version>
< /依赖>
<依赖>
< groupId> org.springframework< / groupId>
< artifactId> spring-web< / artifactId>
< version> $ {spring.version}< / version>
< /依赖>
<依赖>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< version> $ {spring.version}< / version>
< /依赖>
<! - Apache Commons FileUpload - >
<依赖>
< groupId> commons-fileupload< / groupId>
< artifactId> commons-fileupload< / artifactId>
< version> 1.3.2< / version>
< /依赖>
<依赖>
< groupId> commons-io< / groupId>
< artifactId> commons-io< / artifactId>
< version> 2.5< / version>
< /依赖>
HTML
< form method =POSTenctype =multipart / form-dataaction =uploadAction>
< table> < / td>< td>< ;
< tr>< td>< / td>< td>< input type =submitvalue =Upload/>< / td>< / tr>
< / table>
< / form>
Spring上下文
< bean id =multipartResolverclass =org.springframework.web.multipart.commons.CommonsMultipartResolver>
< property name =maxUploadSizevalue =10000000/>
< / bean>
Spring控制器
@PostMapping(/ uploadAction)
public String handleFileUpload(@RequestParam(file)MultipartFile文件,
RedirectAttributes redirectAttributes){
File out =新文件(outputfile.pdf);
FileOutputStream fos = null;
尝试{
fos = new FileOutputStream(out);
//将指定字节数组中的字节写入此文件输出流
fos.write(file.getBytes());
System.out.println(上传和写入输出文件);
catch(FileNotFoundException e){
System.out.println(File not found+ e);
} catch(IOException ioe){
System.out.println(写入文件时出现异常+ ioe);
} finally {
//使用close方法关闭流
尝试{
if(fos!= null){
fos.close();
catch(IOException ioe){
System.out.println(Error Error while closing stream:+ ioe);
}
//storageService.store(file);
redirectAttributes.addFlashAttribute(message,
You successfully uploaded+ file.getOriginalFilename()+!);
返回redirect:/;
}
}
So I'm a newbie to Spring and I'm trying to get file upload working for my project (I'm using Spring Tool Suite btw.) and when submitting a form all I'm getting is:
Stack trace from browser:
type Exception report message Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided description The server encountered an internal error that prevented it from fulfilling this request. exception org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:100) org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.(StandardMultipartHttpServletRequest.java:78) org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:75) org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:108) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106) root cause java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided org.apache.catalina.connector.Request.parseParts(Request.java:2676) org.apache.catalina.connector.Request.getParts(Request.java:2643) org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1083) org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:85) org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.(StandardMultipartHttpServletRequest.java:78) org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:75) org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:108) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106) note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.
This is the form tag in jsp:
<form:form class="form-horizontal" role="form" method="post"
action="newArtist.html" modelAttribute="artist" enctype="multipart/form-data">
Input part:
<div class="form-group">
<div class="col-lg-3">
<label for="photo">Artist photo:</label>
<form:input type="file" id="photo" path="photo"></form:input>
</div>
</div>
Photo is stored in this field in Artist object:
@Lob
private byte[] photo;
Controller mapping methods:
@RequestMapping(value = "/newArtist", method = RequestMethod.GET)
public String showAddArtistForm(Model model)
{
model.addAttribute("artist", new Artist());
return "newArtist";
}
@RequestMapping(value = "/newArtist", method = RequestMethod.POST)
public String addArtist(@ModelAttribute("artist") @Valid Artist artist, BindingResult result,
@RequestParam("photo") MultipartFile photo) throws IOException
{
if (result.hasErrors())
return "newArtist";
if(photo.getBytes() != null)
artist.setPhoto(photo.getBytes());
artistService.addArtist(artist);
return "redirect:artists.html";
}
Multipart resolver configuration in servlet-context.xml:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
Filters in web.xml:
<filter>
<filter-name>MultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>MultipartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Dependencies:
<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
I also imported Tomcat's config file context.xml to META-INF/context.xml and edited Context tag like so:
<Context allowCasualMultipartParsing="true">
Nothing seems to be working, any help will be greatly appreciated.
Actually you don't need any filter on the web.xml in order to upload your multipart file with Spring MVC. I've the same configuration in my project and it worked (${spring.version} = 4.3.4.RELEASE):
POM
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
HTML
<form method="POST" enctype="multipart/form-data" action="uploadAction">
<table>
<tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
Spring context
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
Spring controller
@PostMapping("/uploadAction")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
File out = new File("outputfile.pdf");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(out);
// Writes bytes from the specified byte array to this file output stream
fos.write(file.getBytes());
System.out.println("Upload and writing output file ok");
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while writing file " + ioe);
} finally {
// close the streams using close method
try {
if (fos != null) {
fos.close();
}
} catch (IOException ioe) {
System.out.println("Error while closing stream: " + ioe);
}
//storageService.store(file);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
}
这篇关于Spring MVC文件上传 - 无法处理部分,因为没有提供多部分配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!