java string 乱码处理_java中使用string发生乱码怎么解决

java中使用string发生乱码怎么解决

发布时间:2020-06-26 15:14:18

来源:亿速云

阅读:96

作者:Leah

java中使用string发生乱码怎么解决?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

问题出在预发、生产和本地环境的系统编码方式不一致,本地系统默认是UTF-8,而预发、生产环境默认是GBK编码,因此导致出现乱码。

如果不指定编码方式,则默认以系统的编码方式。String csn = Charset.defaultCharset().name();

try {

// use charset name decode() variant which provides caching.

return decode(csn, ba, off, len);

} catch (UnsupportedEncodingException x) {

warnUnsupportedCharset(csn);

}

try {

return decode("ISO-8859-1", ba, off, len);

} catch (UnsupportedEncodingException x) {

// If this code is hit during VM initialization, MessageUtils is

// the only way we will be able to get any kind of error message.

MessageUtils.err("ISO-8859-1 charset not available: " +

x.toString());

// If we can not find ISO-8859-1 (a required encoding) then things

// are seriously wrong with the installation.

System.exit(1);

return null;

}

System.getProperty("file.encoding") //查看系统默认编码方式

解决方法如下:

1、使用string时进行转码System.out.println(str);

String str1 = new String(str.getBytes("ISO-8859-1"), "utf-8");

System.out.println(str1);

String str2 = new String(str.getBytes("gb2312"), "utf-8");

System.out.println(str2);

String str3 = new String(str.getBytes("gbk"), "utf-8");

System.out.println(str3);

2、将乱码的字符串进行转码String decodeStr=null;

decodeStr = URLDecoder.decode(url, "utf-8");

因此在使用String的时候,无论 encode 或者 decode都要指定编码方式,否则就和系统环境耦合了。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用Zip压缩文文件名可能会出现乱码的问题,这是因为Zip文件格式默认使用的是ASCII编码,而文字符需要使用其他编码方式进行处理解决这个问题的方法有两种: 1. 使用Apache Commons Compress库 可以使用Apache Commons Compress库来压缩文件,这个库支持多种编码方式,可以解决文文件名乱码的问题。示例代码如下: ```java import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; public class ZipUtil { public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipArchiveOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); zos.closeArchiveEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ArchiveEntry entry = zos.createArchiveEntry(new File(file.getPath()), path); zos.putArchiveEntry(entry); InputStream is = new FileInputStream(file); IOUtils.copy(is, zos); is.close(); zos.closeArchiveEntry(); } } } ``` 2. 使用Java的ZipOutputStream类 Java自带的ZipOutputStream类也可以用于压缩文件,但是需要手动设置编码方式,示例代码如下: ```java import java.io.*; import java.nio.charset.Charset; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtil { private static final int BUFFER_SIZE = 2 * 1024; public static void zip(String sourcePath, String destPath) throws IOException { FileOutputStream fos = new FileOutputStream(destPath); ZipOutputStream zos = new ZipOutputStream(fos, Charset.forName("GBK")); File file = new File(sourcePath); String basePath = file.getParent(); compress(file, zos, basePath); zos.close(); fos.close(); } private static void compress(File file, ZipOutputStream zos, String basePath) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files.length == 0) { String path = file.getPath().substring(basePath.length() + 1) + "/"; ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); zos.closeEntry(); } else { for (File f : files) { compress(f, zos, basePath); } } } else { String path = file.getPath().substring(basePath.length() + 1); ZipEntry entry = new ZipEntry(path); zos.putNextEntry(entry); InputStream is = new FileInputStream(file); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = is.read(buffer)) != -1) { zos.write(buffer, 0, len); } is.close(); zos.closeEntry(); } } } ``` 以上两种方法都可以解决文文件名乱码的问题,具体使用哪一种方法可以根据实际需要选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值