Je souhaite télécharger deux fichiers au format HTML, mais je reçois toujours une exception de requête en plusieurs parties. J'ai beaucoup cherché sur Google mais je n'ai pas trouvé la bonne solution.
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.assertIsMultipartRequest(RequestParamMethodArgumentResolver.Java:184)
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.Java:149)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.Java:83)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.Java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.Java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:117)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:820)
at org.Eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.Java:534)
at org.Eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.Java:476)
at org.Eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.Java:119)
at org.Eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.Java:517)
at org.Eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.Java:226)
at org.Eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.Java:935)
at org.Eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.Java:404)
at org.Eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.Java:184)
at org.Eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.Java:870)
at org.Eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.Java:117)
at org.Eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.Java:116)
at org.Eclipse.jetty.server.Server.handle(Server.Java:346)
at org.Eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.Java:596)
at org.Eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.Java:1068)
at org.Eclipse.jetty.http.HttpParser.parseNext(HttpParser.Java:807)
at org.Eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.Java:220)
at org.Eclipse.jetty.server.HttpConnection.handle(HttpConnection.Java:426)
at org.Eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.Java:520)
at org.Eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.Java:40)
at org.Eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.Java:528)
at Java.lang.Thread.run(Thread.Java:680)
Mon contrôleur:
@RequestMapping(value = "blub", method = RequestMethod.POST)
public String String(@RequestParam("version") String version, @RequestParam("changeLog") String changeLog,
@RequestParam("status") String status, @RequestParam("y") MultipartFile y,
@RequestParam("x") MultipartFile x, @RequestParam("zId") String zId, RedirectAttributes attr) {
double size = 0.0;
byte[] xFile = null;
byte[] yFile = null;
String downloadUrl = "blub";
if (!x.isEmpty() && !y.isEmpty()) {
try {
xFile = x.getBytes();
yFile = y.getBytes();
size = x.getSize() / (1024 * 1024);
} catch (IOException e) {
}
System.out.println("file upload received! Name:[" + version + "] Size:[" + xFile.length + "]");
System.out.println("file upload received! Name:[" + version + "] Size:[" + yFile.length + "]");
} else {
return "file upload failed!";
}
zService.saveZ(new Z(changeLog, status, downloadUrl, size, new Date(), yFile, xFile,
Integer.parseInt(zId)));
return "redirect:Z";
}
ma page .jsp:
<form method="POST" action="/form/blub" id="Z" class="form-horizontal" enctype=”multipart/form-data”>
<fieldset>
<legend>Create Z</legend>
<div class="control-group">
<label class="control-label" for="input">Version</label>
<div class="controls">
<input type="text" class="input-xlarge" id="version" name="version">
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea">Changelog</label>
<div class="controls">
<textarea class="input-xlarge" id="textarea" rows="4" id="changeLog" name="changeLog"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input">Status</label>
<div class="controls">
<input type="text" class="input-xlarge" id="status" name="status">
</div>
</div>
<div class="control-group">
<label class="control-label" for="select01">Select list</label>
<div class="controls">
<select id="Z" name="Z">
<c:forEach items="${z}" var="current">
<option value="${z.id}">
<c:out value="${z.name}" />
</option>
</c:forEach>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="x">File input</label>
<div class="controls">
<input class="input-file" id="x" type="file">
</div>
</div>
<div class="control-group">
<label class="control-label" for="y">File input</label>
<div class="controls">
<input class="input-file" id="y" type="file">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Create Z</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
spring-serverlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<!-- use this for Spring Jackson JSON support -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<tx:annotation-driven transaction-manager="transactionManager" />
<jdbc:embedded-database id="dataSource" type="H2" />
<context:component-scan base-package="com.x.y.z.config" />
<context:component-scan base-package="com.x.y.z.market.dao" />
<context:component-scan base-package="com.x.y.z.service.app" />
<context:component-scan base-package="com.x.y.z.controller" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
</bean>
</beans>
Essayez d'ajouter les en-têtes:
@RequestMapping(value=("/uploadpsd"),headers=("content-type=multipart/*"),method=RequestMethod.POST)
Vous devez ajouter ceci à votre printemps xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
Assurez-vous que vous utilisez les guillemets corrects pour l'attribut enctype
- de l'élément form
- dans votre jsp.
1 .enctype=”multipart/form-data”
2 .enctype="multipart/form-data"
observez les guillemets doubles ici il y a une différence entre les deux ... utilisez enctype="multipart/form-data"
que enctype=”multipart/form-data”