URLDecoder: Illegal hex characters in escape (%) pattern for multipart data

≯℡__Kan透↙ 提交于 2019-12-13 03:45:14

问题


I am using multipart to fetch the pdf and object data from the service. I get the below error

java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "PD" at java.net.URLDecoder.decode(URLDecoder.java:205) at org.springframework.http.converter.FormHttpMessageConverter.read(FormHttpMessageConverter.java:186)

when invoking the service.

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 .

Anything I am missing here?


回答1:


You try to read from FormHttpMessageConverter but the doc https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/FormHttpMessageConverter.html says

"In other words, this converter can read and write the "application/x-www-form-urlencoded" media type as MultiValueMap and it can also write (but not read) the "multipart/form-data" media type as MultiValueMap."



来源:https://stackoverflow.com/questions/53340819/urldecoder-illegal-hex-characters-in-escape-pattern-for-multipart-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!