i have problem when i'm posting multipart/form-data with utf-8 content from android.
this is fragment from my AsyncTask for posting data to server. Everything works fine except utf string data. server receives "????????" < question marks instead of utf-8 text.
RestTemplate restTemplate = new RestTemplate();
FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
//formConverter.setCharset(Charset.forName("UTF8")); // don't works
restTemplate.getMessageConverters().add(formConverter);
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Cookie", "my cookie string");
// don't works
//Charset charset = Charset.forName("UTF8");
//ArrayList<Charset> acceptCharset = new ArrayList<>();
//acceptCharset.add(charset);
//httpHeaders.setAcceptCharset(acceptCharset);
File f = new File(this.caller.mainActivity.getApplicationContext().getCacheDir(), "picture");
f.createNewFile();
OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
caller.mainActivity.capturedBitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
os.close();
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("picture", new FileSystemResource(f.getPath()));
map.add("categoryId", "1");
map.add("description", "text with utf-8 content ტესტ . test");
HttpEntity<MultiValueMap<String, Object>> requestDataEntity = new HttpEntity<>(map, httpHeaders);
HttpEntity<HashMap> response = restTemplate.exchange("url.of.server", HttpMethod.POST, requestDataEntity, HashMap.class);
server has no problem. i tested it from postman and server can receive multipart/form-data with utf-8 content.
on server value of "description" is: "text with utf-8 content ???? . test"
Server Side - fragment of route
var express = require('express');
var multer = require('multer');
var upload = multer({ limits: { fileSize: 2000000, files: 1 }, dest: 'tempFiles/' });
....
....
router.post('/add', app.filters.commons.sessionRequired(), upload.single('picture'), function (req, res, next) {
activityBL.add(req, function (error, data) {
error ? next(error) : res.json(data);
});
});
....
....
activityBL - fragment of Business Logic
....
....
bl.add = function (req, cb) {
var file = req.file || null;
if(!file) {
cb({errors: [{code: "004", keyword: "PICTURE_REQUIRED"}]}, null);
return;
}
/*
* retrieve request data
*/
var requestData = JSON.parse(JSON.stringify(req.body));
var desc = requestData.description;
....
....
....
}
Possibly you need to set header to display the utf-8 characters.
header("Content-Type", "application/json; charset=utf-8");
Kindly refer this link : http://expressjs.com/en/api.html will you give you better idea.
Make sure the tables and DB you fetch also applies UTF-8 collation.
Best Luck..!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.