用JSP讀取文件並顯示時出現亂碼的解決過程

今天用JSP讀取本地文件(中文)並顯示到瀏覽器上時出現亂碼,開始覺得是JSP的問題: html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
就改這裏面的的charset和pageEncoding,結果改了仍是錯,而後直覺告訴我錯誤不是這裏,應該是讀文件的問題,改正事後果真一切正常。 java

<% 瀏覽器

String spath = request.getRealPath("/");
String sfilename = "test.txt";

FileInputStream fr = null;
File file = new File(spath+sfilename);
BufferedReader brd = null;
fr = new FileInputStream(file);
brd = new BufferedReader(new InputStreamReader(fr,"UTF-8"));
StringBuffer resultBuffer = new StringBuffer();
while(brd.readLine() != null)
{
String fileline = brd.readLine();
    resultBuffer = resultBuffer.append(fileline);
}
out.println(resultBuffer);
app

相關文章
相關標籤/搜索