我正在使用multipart从服务中获取pdf和对象数据。我收到以下错误
java.lang.IllegalArgumentException:URLDecoder:非法的十六进制字符 以逸出(%)模式显示-对于输入字符串:“ PD”位于 java.net.URLDecoder.decode(URLDecoder.java:205)在 org.springframework.http.converter.FormHttpMessageConverter.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 .
这里我想念什么吗?
答案 0 :(得分:1)
您尝试读取FormHttpMessageConverter,但文档https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/FormHttpMessageConverter.html表示
“换句话说,此转换器可以读取和写入” Application / x-www-form-urlencoded“媒体类型为MultiValueMap,也可以写入(但不读取) / form-data“媒体类型为MultiValueMap。”