java中打开含有中文的文件乱码问题以及jsp中url传递中文乱码问题

String Data_Path = "D:/file_jiucool.txt";  
        File file = new File(Data_Path);  
        String line= "";  
        if(file.exists()){        
            FileReader reader = new FileReader(Data_Path);    
            BufferedReader bufferedreader= new BufferedReader(reader);  
            while((line = bufferedreader.readLine())!=null){  
                    line = new String(line.getBytes(), "UTF-8");  
                    System.out.println(line);  
                }  
        } 

上述代码中,设置为GBK、GB2312 时全部为乱码,设置为UTF-8时,最后一个汉字为乱码,是不是很让人郁闷?正确代码如下:

String Data_Path = "D:/file_jiucool.txt";  
        File file = new File(Data_Path);  
        String line= "";  
        if(file.exists()){        
            //FileReader reader = new FileReader(Data_Path);      
            //BufferedReader bufferedreader= new BufferedReader(reader);  
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");  
            BufferedReader bufferedreader = new BufferedReader(isr);  
            while((line = bufferedreader.readLine())!=null){  
                    //line = new String(line.getBytes(), "UTF-8");  
                    System.out.println(line);  
                }  
        } 


所以,在读取文件的时候就得进行编码设置,而不是读完文件输出时再设置编码!

jsp url传递中文乱码问题,主要是将服务器配置为支持中文的编码格式。然后再在request页面中设置编码格式。

jsp URL传递参数中文乱码的问题

1.在sever.xml中修改成

<Connector port="8080" connectionTimeout="20000" protocol="HTTP/1.1" redirectPort="8443" URLEncoding="GB2312" useBodyEncodingForURI="true" />

2.看我的示例:URLCharset.jsp

<%@ page language="java" contentType="text/html; charset=GB2312" import ="java.util.*"
    pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>URL传递参数中文处理示例</title>
</head>
<%
String param = request.getParameter("param");
%>
<body>
<a href="URLCharset.jsp?param=中国人">请点击这个链接</a><br>
你提交的参数是:<%=param %><br>
<%out.print("中文") ;%>
</body>

3.结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值