【发布时间】:2015-06-03 13:53:16
【问题描述】:
我用一个方法根据Locale.getDefault()得到一个月的显示名
private String getLocaleMonthString(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
我以http request 发送此结果,http request 编码为UTF-8
显示名称是???对于某些语言,即使在我的日志中它是正确的。
问题是string 不是使用正确的编码创建的,但我看不到在这小段代码中我可以在哪里更改/设置编码?
编辑:
按要求添加了 httppost 的代码
Date date = null;
HttpPost post = null;
MultipartEntityBuilder builder = null;
builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(Charset.forName(HTTP.UTF_8));
date = getDateFromFile(file,0);
String localeMonth = getLocaleMonthString(date);
post = new HttpPost(url);
builder.addPart("filePath", new StringBody(file.getAbsolutePath(),ContentType.TEXT_PLAIN));
builder.addPart("date", new StringBody(getISODate(date), ContentType.TEXT_PLAIN));
builder.addPart("localeMonth", new StringBody(localeMonth, ContentType.APPLICATION_FORM_URLENCODED));
builder.addPart("type", new StringBody("video", ContentType.TEXT_PLAIN));
HttpResponse response = null;
【问题讨论】:
-
字符串本身没有编码。当字符串中的字符转换为字节(在 HTTP 响应中)时,就会发生编码。
-
"对于某些语言,显示名称是 ???" ... 对于哪些语言、哪些字段/样式以及在哪些浏览器上会发生这种情况?
-
语言是希伯来语,在 Firefox 上。字段/样式?我猜的默认值
-
字符串编码不能不正确。问题肯定不在于这段代码,而在于稍后将这个字符串写入 servlet
OutputStream时的某个地方。 -
您能否发布创建/发送包含月份字符串的 HTTP 请求的代码?
标签: java utf-8 httprequest encode