java读取TXT文件显示在浏览器上,中文内容是乱码,求教~

代码如下。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("fileName>>>" + fileName);
String filePath = "D://work//" + fileName;
File obj = new File(filePath);
if (!obj.exists()) {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print("指定文件不存在!");
return;
}
ServletOutputStream out = response.getOutputStream();
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="
+ obj.getName().getBytes("GB2312"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(filePath));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}
}

第1个回答  2013-08-09
用过滤器啊 网上代码多的很 随便找一个就是 页面上的格式用UTF-8
第2个回答  2013-08-09
看一下文本保存的格式是不是UTF-8,还有页面的编码格式。追问

文本格式是ANSI 页面是utf-8

本回答被提问者采纳
相似回答