InputStreamReader引发的一个编码问题
2016-07-22 09:02
435 查看
最近有一个任务,需要接收web前端发送的一串java源代码,然后编译在返回结果以显示,但是一直遇到了编码问题,流程如下
项目都是utf-8编码,
前端以utf-8编码发送源文件,http协议控制如下
然后以utf-8编码保存到本地
使用javac编译
String command = "javac -encoding utf-8 "+ path;
在转换错误流的时候,br = new BufferedReader(new InputStreamReader(is)); br = new BufferedReader(new InputStreamReader(is, "utf8"));这2种方式都出现乱码。
使用utf-8出现错误,猜想错误码返回的是gbk字节。
第一种没指定乱码我想当然的以为是使用本地的默认编码gbk,通过查看源码才发现,如果没用指定编码,使用的是Charset.defaultCharset().name(),输出之后发现竟然不是gbk。。。
项目都是utf-8编码,
前端以utf-8编码发送源文件,http协议控制如下
$.ajax({ type : "POST", url: "compileJava", contentType: "application/json; charset=utf-8", dataType: "json", headers: { Accept: "application/json" }, data: JSON.stringify({"code": code}), success : function(data) { alert(data.msg); $("#response").html(data.msg); } });
然后以utf-8编码保存到本地
public String savaJava(String javaSrc, String path) { FileOutputStream outputStream = null; try { path = path+File.separator+"Main.java"; byte[] bytes = javaSrc.getBytes("utf-8"); outputStream = new FileOutputStream(path); outputStream.write(bytes); FileInputStream input = new FileInputStream(path); BufferedReader bf = new BufferedReader(new InputStreamReader(input, "utf-8")); String ss; while ((ss = bf.readLine()) != null) { System.out.println(ss); } return path; } catch (IOException e) { e.printStackTrace(); }finally { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
使用javac编译
String command = "javac -encoding utf-8 "+ path;
public String compile(String command) throws Exception{ System.out.println("开始编译"+command); Process process = null; try { process = Runtime.getRuntime().exec(command); } catch (Exception e) { return "系统正在维护中"; } InputStream is = null; BufferedReader br = null; try { is = process.getErrorStream(); StringBuilder sb = new StringBuilder(); br = new BufferedReader(new InputStreamReader(is)); String line = null; System.out.println("F" + Charset.defaultCharset().name()); while ((line = br.readLine()) != null) { sb.append(line); System.out.println(line); sb.append("\n"); } process.waitFor(); return sb.toString(); } catch (Exception e) { throw e; } finally { try { if (null != is) { is.close(); is = null; } if (null != br) { br.close(); br = null; } if (null != process) { process.destroy(); process = null; } } catch (Exception e) { throw e; } } }
在转换错误流的时候,br = new BufferedReader(new InputStreamReader(is)); br = new BufferedReader(new InputStreamReader(is, "utf8"));这2种方式都出现乱码。
使用utf-8出现错误,猜想错误码返回的是gbk字节。
第一种没指定乱码我想当然的以为是使用本地的默认编码gbk,通过查看源码才发现,如果没用指定编码,使用的是Charset.defaultCharset().name(),输出之后发现竟然不是gbk。。。
相关文章推荐
- @RequestBody获取Json请求数据
- 基于cordova插件的上传下载
- js生成随机数和把日期格式转换成yyyyMMddHHmmss的方法
- BZOJ 1025 游戏 DP+lcm+素数筛选
- 【codeforces】Bulbs
- 不要问我这是什么!!!
- BZOJ 1006 神奇的国度 弦图最小染色 MCS算法
- BZOJ 1009 GT考试 DP+矩阵快速幂
- Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
- BZOJ 1056 排名系统 Splay
- MyEclipse:(5)MyEclipse中添加user library
- UTF-8编码(转)
- nginx安装
- Java内存泄露原因详解(转)
- Ubuntu SVN服务器搭建
- BZOJ 1211 树的计数 Prufer序列
- BZOJ 1189 紧急疏散evacuate 二分+BFS+最大流
- solrj定时增量更新索引
- BZOJ 1143 祭祀river 最长反链
- CocoaPods安装和使用