我在网上找了一个SERVLET,自己改了下,但是只能获取数字、英文值,遇到中文就是乱码,求助各位大大,帮忙解决一下,在线等!下面是代码:
private byte[] b = new byte[4096];
private Hashtable fields = new Hashtable();
public void setSourceFile(HttpServletRequest request) throws IOException {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("name=\"")) != -1) {
String fieldName = s.substring(k + 6, s.length() - 3);
sis.readLine(b, 0, b.length);
StringBuffer fieldValue = new StringBuffer(b.length);
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a - 2);
if ((b[0] == 45) && (b[1] == 45) && (b[2] == 45)
&& (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldValue.append(s);
}
}
fields.put(fieldName, fieldValue.toString());
}
if (!successful)
break;
}
}
public String getFieldValue(String fieldName) {
if (fields == null || fieldName == null) {
return null;
}
return (String) fields.get(fieldName);
}
------解决方案--------------------------------------------------------
获取file以外的参数值得话使用request.getParameter("表单中的name值");
中文乱码的话重新编码就可以了,编码方式很多种,按你的需求来看
String result = new String(sourceStr.getBytes("ISO-8859-1"), "UTF-8");应该就可以了
------解决方案--------------------------------------------------------
String result=requestScope.getParameter("表单的name");
String result = new String(sourceStr.getBytes("ISO-8859-1"), "UTF-8");
------解决方案--------------------------------------------------------
tomcat的话可以设置一下服务器编码试试(conf/server.xml)
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
requset.setCh.... utf-8
response.setCharacter utf-8
统一编码