-->

URLDecoder:在逃逸非法十六进制字符(%)为多的数据模式(URLDecoder: Illeg

2019-11-05 07:30发布

我使用多来从服务PDF和对象数据。 我得到下面的错误

java.lang.IllegalArgumentException异常:URLDecoder:在逃逸非法十六进制字符(%)图案 - 对于输入字符串:在org.springframework.http.converter.FormHttpMessageConverter “PD” 在java.net.URLDecoder.decode(URLDecoder.java:205) .read(FormHttpMessageConverter.java:186)

调用服务时。

 SERVICE : @RequestMapping(value = "/getPDF", method = RequestMethod.GET,produces = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<MultiValueMap<String, Object>> getPDF( @RequestParam String key, HttpServletResponse response) { MultiValueMap<String, Object> pdfResultMap = new LinkedMultiValueMap<String, Object>(); //Get the result ByteArrayResource byteArrayResource = getPdf(); //Assign the PDF //1) Build the first byte[] result /* LinkedMultiValueMap<String, String> pdfMap = new LinkedMultiValueMap<>(); pdfMap.add("Content-disposition", "attachment;" ); pdfMap.add("Content-type", "application/pdf");*/ HttpHeaders xHeader2 = new HttpHeaders(); xHeader2.setContentType(MediaType.APPLICATION_PDF); HttpEntity<ByteArrayResource> doc = new HttpEntity<ByteArrayResource>(byteArrayResource, xHeader2); pdfResultMap.add("doc", doc); // 2) Build the next //Header HttpHeaders xHeader = new HttpHeaders(); xHeader.setContentType(MediaType.APPLICATION_JSON); // Get the result Map<String, String> stringMap = new HashMap<String, String>(); //populate String map HttpEntity<Map<String, String>> stringMapObject = new HttpEntity<Map<String, String>>(stringMap, xHeader); pdfResultMap.add("stringMap", stringMapObject); //3) Build the simple header HttpHeaders xHeader1 = new HttpHeaders(); xHeader.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> titlePart = new HttpEntity<String>("pdftitle", xHeader1); pdfResultMap.add("title", titlePart); ResponseEntity<MultiValueMap<String, Object>> responseEntity = new ResponseEntity<MultiValueMap<String, Object>>(pdfResultMap, HttpStatus.OK); return responseEntity; } CLIENT : public getPdf() { FormHttpMessageConverter formConverter = new FormHttpMessageConverter() { @Override public boolean canRead(Class<?> clazz, MediaType mediaType) { if (clazz == MultiValueMap.class) { return true; } return super.canRead(clazz, mediaType); } }; formConverter.setCharset(Charset.forName("UTF-8")); List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>(); partConverters.add(new ByteArrayHttpMessageConverter()); StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8")); stringHttpMessageConverter.setWriteAcceptCharset(false); partConverters.add(stringHttpMessageConverter); partConverters.add(new ResourceHttpMessageConverter()); formConverter.setPartConverters(partConverters); restTemplate.getMessageConverters().add(formConverter); ResponseEntity<MultiValueMap> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,entity, MultiValueMap.class); } I Tried adding : List<MediaType> a = new ArrayList<MediaType>(); a.add(MediaType.APPLICATION_OCTET_STREAM); a.add(MediaType.MULTIPART_FORM_DATA); a.add(new MediaType("application","pdf")); formConverter.setSupportedMediaTypes(a); But the same error . 

什么我在这里失踪?

Answer 1:

您尝试从FormHttpMessageConverter阅读,但DOC https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/FormHttpMessageConverter.html说

“换言之,该转换器可以读取和写入‘应用/ X WWW的窗体-urlencoded’媒体类型如MultiValueMap和它也可以写入(但不读取)的‘多部分/格式数据’媒体类型如MultiValueMap。 “



文章来源: URLDecoder: Illegal hex characters in escape (%) pattern for multipart data