使用Content-Type发送PUT请求:application / x-www-form-urlencoded并发送图像文件不起作用

时间:2017-11-16 08:22:50

标签: spring-mvc

当我使用内容类型发送PUT请求时:application / x-www-form-urlencoded 和错误的内容,如图片;该请求未被服务器正确处理,并且在没有服务器处理的情况下发回以下异常:

  

错误; servlet [dispatcherServlet]的Servlet.service()与path []的上下文引发了异常;   java.lang.IllegalArgumentException:URLDecoder:转义(%)模式中的非法十六进制字符 - 对于输入字符串:“h ”       在java.net.URLDecoder.decode(URLDecoder.java:194)       在org.springframework.http.converter.FormHttpMessageConverter.read(FormHttpMessageConverter.java:241)       在org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:100)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)       在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)       在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:798)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1434)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:745)

他们在Spring的任何方式我们可以处理这个场景吗?

1 个答案:

答案 0 :(得分:0)

文件需要是multipart / form-data content-type

你基本上有三个选择:

  1. Base64对文件进行编码,代价是将数据大小增加约33%。

  2. 首先在multipart / form-data POST中发送文件,然后将ID返回给客户端。然后客户端发送带有ID的元数据,服务器重新关联文件和元数据。

  3. 首先发送元数据,然后将ID返回给客户端。然后客户端发送带有ID的文件,服务器重新关联文件和元数据。