restTemplate 发送 MultiValueMap 中文乱码 (FormHttpMessageConverter 编码方式)
=====================================
// 设置 resttemplate formhttpmessageconverter 编码方式
@configuration
public class resttemplateconfig {
@bean
resttemplate resttemplate(){
simpleclienthttprequestfactory requestfactory = new simpleclienthttprequestfactory();
requestfactory.setconnecttimeout(60000);
requestfactory.setreadtimeout(60000);
resttemplate resttemplate = new resttemplate(requestfactory);
// 设置 resttemplate formhttpmessageconverter 编码方式
for (httpmessageconverter<?> httpmessageconverter : list) {
if(httpmessageconverter instanceof formhttpmessageconverter) {
((formhttpmessageconverter) httpmessageconverter).setcharset(charset.forname(standardcharsets.utf_8.tostring()));
}
}
resttemplate.setmessageconverters(list);
return resttemplate;
}
}
==============================
@service
public class fileservice implements ifileservice{
@autowired
private resttemplate resttemplate;
@override
public void uploadfile(multipartfile file) throws ioexception {
bytearrayresource bytearrayresource = new bytearrayresource(file.getbytes()) {
@override
public string getfilename() {
return file.getoriginalfilename();
};
};
multivaluemap<string, bytearrayresource> map = new linkedmultivaluemap<string, bytearrayresource>();
map.add("file", bytearrayresource);
resttemplate.postforentity("http://localhost:8066/file/upload", map, string.class);
}
}
上一篇: CAD两条线段不相交该怎么裁剪及延伸?
下一篇: 可穿戴设备玩支付:能否干掉二维码?