赞
踩
今天做上传功能代码读取返回值得时候,StringBuilder.toString()出现乱码的情况,在网上找了一堆乱七八糟的办法,繁琐不说,代码还好几行,后来看到String类构造自己就有办法解决这个问题,简单一行代码搞定,在这里记录一下:
- StringBuilder sb2 = new StringBuilder();
- String succeedStr = new String(sb2.toString().getBytes("iso8859-1"),"UTF-8");
加上上面那句代码,ok,解决!!! so easy, so beautiful !!!
下面贴出网上流传的方法:
- public String convertStreamToString(InputStream is) {
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(is)<span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);">,"UTF-8"</span>);
- StringBuilder sb = new StringBuilder();
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
- sb.append(line + "/n");
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return sb.toString();
- }
这么多代码是要闹哪样?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。