spring restTemplate 上传文件乱码问题

 

FormHttpMessageConverter 的

 
protected byte[] getAsciiBytes(String name) {
try {
return name.getBytes("US-ASCII");   //改为 UTF-8 就OK 了。
}
catch (UnsupportedEncodingException ex) {
// should not happen, US-ASCII is always supported
throw new IllegalStateException(ex);
}
}

 

 

 

 

 

	   HttpHeaders requestHeaders = new HttpHeaders();
	// Sending multipart/form-data
	   requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
	   
	   HttpHeaders headers = new HttpHeaders();
	   headers.setContentType(new MediaType("text" ,"plain",Charset.forName("UTF-8")));
	   HttpEntity entity = new HttpEntity("测试一下", headers);
	   MultiValueMap formData = new LinkedMultiValueMap();
	   
	   formData.add("caption",entity);
		
	   HttpHeaders headers1 = new HttpHeaders();
	   headers1.setContentType(new MediaType("application" ,"octet-stream",Charset.forName("UTF-8")));
//       xmlHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
       HttpEntity fileEntity = new HttpEntity(resource, headers1);
	   formData.add("file", fileEntity);
	   
	   
	   HttpEntity> requestEntity = new HttpEntity>(formData, requestHeaders);

	   ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
 

 

 

你可能感兴趣的:(spring restTemplate 上传文件乱码问题)