上传文件乱码,GBK转UTF-8
2016-01-29 10:17
477 查看
最近遇到这样一个问题,我上传文件没有转换字符集windows默认GBK,结果当我需要读文件显示时用UTF-8转当然会出现乱码,为了解决这个问题,我就将上传时文件一同也转成UTF-8了这样就不会乱码了,上传时转换字符集方法如下:
String root = filePath;// 上传路径 File rootFile = new File(root); // 路径不存在先创建路径 if (!rootFile.exists()) { rootFile.mkdirs(); } // 获取后缀type String suffix = domain.getSuffixName(); // 新文件名 String strNewName = domain.getScriptName().trim() + domain.getVersionName().trim() + "." + suffix; domain.setScriptUrl("/" + strNewName); File destFile; String destFilePath=root+"/"+strNewName; destFile = new File(root, strNewName); OutputStream outputStream; outputStream = new FileOutputStream(destFile); // file是上传过来的文件,存放为临时tmp File file = domain.getFile(); String context = ""; InputStreamReader isr; isr = new InputStreamReader(new FileInputStream(file), "GBK"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { context += line + "\r\n"; System.out.println(line); } br.close(); byte[] content = context.getBytes(); context = new String(content, "UTF-8"); BufferedWriter writer; FileOutputStream fos = new FileOutputStream(destFilePath, true); System.out.println(root+"/"+strNewName); writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")); writer.append(context); writer.close();
相关文章推荐
- Python twisted article
- Mongodb集群搭建的三种方式
- selenium_python系列之二 Python启动浏览器Firefox\Chrome\IE
- AndroidStudio快捷键汇总
- nginx中的ngx_cdecl
- 1月末中国域名商解析量13强:西数破百万指日可待
- mysql笔记
- 卷积神经网络Lenet-5实现
- 扣丁学堂——布局性能优化(Hierarchy Viewer工具)
- 操作系统学习基本概念汇总
- java模拟实现生产者---消费者问题
- 大道至简——书摘与思考[二]
- JAVA的BIT数组
- web移动端常见问题解决方案
- PLSQL快捷键设置
- [Shader 着色器] 学习前了解知识
- 基于遗传算法的寻路模拟
- iOS 动画之购物车 贝塞尔曲线
- Linux/Mac 常用命令
- 让iOS程序可以和 iTunes 进行文件交互