上传文件乱码,GBK转UTF-8

最近遇到这样一个问题,我上传文件没有转换字符集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();

 

转载于:https://www.cnblogs.com/dylanblog/p/5168162.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值