如何要求提供虚假的多部分文件列表?

时间:2019-05-02 05:06:13

标签: java spring-mvc

我有一个pojo,其中包含图像列表。 在保存数据时,我没有选择要保存的任何图像,它向我返回了控制器中的类型未命中匹配错误。 我想要对包含多部分文件的图像列表进行错误处理。 我该怎么办?

*****************************错误***************** ************************ [http-nio-8087-exec-11]错误com.iconicus.error.restapp.RestResponseEntityExceptionHandler-400状态码 org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:2个错误 字段'imageses [0] .productimage'上的对象'productform'中的字段错误:拒绝的值[];代码[typeMismatch.productform.imageses [0] .productimage,typeMismatch.productform.imageses.productimage,typeMismatch.imageses [0] .productimage,typeMismatch.imageses.productimage,typeMismatch.productimage,typeMismatch.org.springframework.web.multipart。 MultipartFile,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[productform.imageses [0] .productimage,imageses [0] .productimage];参数[];默认消息[imageses [0] .productimage]];默认消息[未能将属性'imageses [0] .productimage'的类型[java.lang.String]的属性值转换为所需的类型[org.springframework.web.multipart.MultipartFile];嵌套异常是java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为属性“ productimage”的所需类型[org.springframework.web.multipart.MultipartFile]:找不到匹配的编辑器或转换策略] 字段'imageses [1] .productimage'上的对象'productform'中的字段错误:拒绝的值[];代码[typeMismatch.productform.imageses [1] .productimage,typeMismatch.productform.imageses.productimage,typeMismatch.imageses [1] .productimage,typeMismatch.imageses.productimage,typeMismatch.productimage,typeMismatch.org.springframework.web.multipart。 MultipartFile,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[productform.imageses [1] .productimage,imageses [1] .productimage];参数[];默认消息[imageses [1] .productimage]];默认消息[未能将属性'imageses [1] .productimage'的类型[java.lang.String]的属性值转换为所需的类型[org.springframework.web.multipart.MultipartFile];嵌套异常是java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为属性“ productimage”的所需类型[org.springframework.web.multipart.MultipartFile]:找不到匹配的编辑器或转换策略]     在org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:118)     在org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)     在org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)     在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)     在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)     在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在com.iconicus.app.filter.FebnexFilter.doFilter(FebnexFilter.java:112)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:317)     在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)     在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)处     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)处     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)     在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)处     在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)     在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)     在org.springframework.security.web.debug.DebugFilter.invokeWithWrappedRequest(DebugFilter.java:90)     在org.springframework.security.web.debug.DebugFilter.doFilter(DebugFilter.java:77)     在org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)     在org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)     在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)     在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)     在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)     在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)     在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)     在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:748)

*************************************控制器********* ******************

@RequestMapping(value = {"/updateimages"}, method = RequestMethod.POST)
@ResponseBody
public ApiResponse updateImages(@ModelAttribute("productform") Product productForm, HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, IOException {
    int responsedata = 0;
    try {
        productService.updateProductImages(productForm);
    } catch (ClassNotFoundException | SQLException ex) {
        logger.error(ex.getMessage(), ex);
    }
    return new ApiResponse(HttpStatus.OK, "Product Updated", String.valueOf(productForm.getId()), true);
}

***********************************产品(POJO)******** **************

公共类产品{

private String id;
private String productname;
private String displayname;
private String productprice;
private String category;
private String subcategory;
private String categoryname;
private String subcategoryname;
private String taxcategory;
private String productweight;
private String description;
private String productsortorder;
private String productsku;
private String productshippingweight;
private String fixshippinginindia;
private String fixshippingworldwide;
private String allowcod;
private String visible;
private String active;
private String allowpurchaseind;
private String allowpurchaseworldwide;
private String freeshippinginindia;
private String freeshippingworldwide;
private String shippingasperweigth;
private String[] tags;
private ProductSeo productSeo;
private ProductOption productOption;
private List<ProductOptionSet> optionSets;
private List<ProductImages> imageses;
private List<ProductGroupTag> groupTag;
private List<ProductSeoDetail> seodetails;
private List<NoteForm> noteForm;

}

****************************产品图片(POJO)************** **********

公共类ProductImages {

private String id;
private String productid;
private MultipartFile productimage;
private String productimagename;
private String imagetitle;
private String imagedescription;
private String imageseotitle;
private String imageseokeyword;
private String imageseodescription;
private String fileaction;

}

1 个答案:

答案 0 :(得分:1)

检查是否已启用MultipartResolver。您可以使用CommonsMultipartResolver处理文件上传。然后下面的代码片段就可以满足需要:

public String fileHandleController(HttpServletRequest request, @ModelAttribute Pojo pojo) {
    //check your request is multipart
    String contentType = request.getContentType();
    if(contentType != null && contentType.indexOf("multipart/form-data") > -1) {
        MultipartFile multipartFile = pojo.getFile();
        if(multipartFile.getSize() > 0){
            //to do
        }
    }

}