gpt4 book ai didi

java - 网址解码器 : Illegal hex characters in escape (%) pattern for multipart data

转载 作者:行者123 更新时间:2023-12-02 10:35:15 26 4
gpt4 key购买 nike

我正在使用 multipart 从服务中获取 pdf 和对象数据。我收到以下错误

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)

调用服务时。

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 .

我在这里缺少什么吗?

最佳答案

您尝试从 FormHttpMessageConverter 读取内容,但文档 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/FormHttpMessageConverter.html

“换句话说,此转换器可以将“application/x-www-form-urlencoded”媒体类型读取和写入为 MultiValueMap,并且还可以写入(但不能读取)“multipart”/form-data"媒体类型为 MultiValueMap。"

关于java - 网址解码器 : Illegal hex characters in escape (%) pattern for multipart data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com