日期:2014-05-17  浏览次数:20821 次

JAVA下载LINUX的TXT文件,文件内容中文乱码
在linux里有个txt文件,名称为11.txt,里面的内容为中文,比如“中文12345abc”;
我的部分下载代码如下:
FileInputStream fs = null;
try
{
    fs = new FileInputStream(new File(filePath));
}
catch (FileNotFoundException e)
{
    e.printStackTrace();
    return;
}
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName+ "\"");
int b = 0;
try
{
PrintWriter out = response.getWriter();
while ((b = fs.read()) != -1)
{
out.write(b);
}
fs.close();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
现在的问题是,下载到windows中,这个11.txt打开之后,里面的中文就出现乱码了。
麻烦各位高手给帮忙看看……
------解决方案--------------------
跟代码无关,是你linux和windows的编码不一致导致乱码

乱码=用A编码,用B解码,就会出现乱码,你的文件传输过程和编码无关,是二进制流传输
------解决方案--------------------
PrintWriter out = response.getWriter();
这里的问题,Writer基本上都是基于文本的,很容易出现乱码,还是用stream吧。
response.getOutputSeream(),这样,读的时候也用字节读就好了