当前位置:编程学习 > JAVA >>

file.getName()的编码是怎么样确定的

服务器是suse
LANG=zh_CN.GBK
我写了一个JSP页面,遍历某个目录下的所有文件,结果读出来的中文文件名是全是乱码。
我的页面设置如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

response.setContentType("text/html;charset=UTF-8 ");

tomcat设置也是UTF-8
尝试过的手段有

String fName = new String(file.getName().getByte(), "UTF-8");
String fName = new String(file.getName().getByte(), "GBK");
//其它相似代码省略……

也修改过服务器的LANG,曾设置为LANG=zh_CN.UTF-8,也没有任何效果。

现在这个问题已经搞得我抓狂了,特来求教。 中文乱码 编码 --------------------编程问答-------------------- String fName = new String(file.getName().getBytes("iso-8859-1"), "UTF-8");
这样试试 --------------------编程问答-------------------- String fName = new String(file.getName().getBytes("iso-8859-1"), "UTF-8"); --------------------编程问答--------------------
String fName = new String(file.getName().getByte("GBK"), "UTF-8");
String fName = new String(file.getName().getByte("UTF-8"), "GBK");
--------------------编程问答--------------------
引用 1 楼 suciver 的回复:
String fName = new String(file.getName().getBytes("iso-8859-1"), "UTF-8");
这样试试


引用 2 楼 u011248395 的回复:
String fName = new String(file.getName().getBytes("iso-8859-1"), "UTF-8");


引用 3 楼 huxiweng 的回复:
String fName = new String(file.getName().getByte("GBK"), "UTF-8");
String fName = new String(file.getName().getByte("UTF-8"), "GBK");


唉……
无论getByte()里填什么都没效果 --------------------编程问答-------------------- 上面已有答案了。 --------------------编程问答--------------------
引用 5 楼 licip 的回复:
上面已有答案了。

无效的答案,毫无意义 --------------------编程问答--------------------

import java.net.URLEncoder;
private String encodeFileName(HttpServletRequest request,String fileName){  
        try{  
            //IE  
            if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") >0){  
                fileName=URLEncoder.encode(fileName,"UTF-8");  
            }else{  
                fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return fileName;  
    }


使用这个方法,前几个月写过上传的模块,亲测有用。 --------------------编程问答-------------------- response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "iso-8859-1"));
这是我下载文件设置的文件名! --------------------编程问答--------------------
引用 7 楼 yousteely 的回复:

import java.net.URLEncoder;
private String encodeFileName(HttpServletRequest request,String fileName){  
        try{  
            //IE  
            if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") >0){  
                fileName=URLEncoder.encode(fileName,"UTF-8");  
            }else{  
                fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return fileName;  
    }


使用这个方法,前几个月写过上传的模块,亲测有用。

引用 8 楼 huixiangdeqiji 的回复:
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "iso-8859-1"));
这是我下载文件设置的文件名!

无语了,一群人回帖不看帖的么,还是我的问题表达不清楚
我没有做任何地页面跳转传参行为,只是单纯地要读取服务器指定目录下的中文文件名!
现在file.getName()显示出来的是乱码,getByte()无论转成UTF-8,GBK,ISO-8898-1都没有任何效果 --------------------编程问答--------------------
引用 9 楼 myyugioh 的回复:
Quote: 引用 7 楼 yousteely 的回复:


import java.net.URLEncoder;
private String encodeFileName(HttpServletRequest request,String fileName){  
        try{  
            //IE  
            if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") >0){  
                fileName=URLEncoder.encode(fileName,"UTF-8");  
            }else{  
                fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return fileName;  
    }


使用这个方法,前几个月写过上传的模块,亲测有用。

引用 8 楼 huixiangdeqiji 的回复:
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "iso-8859-1"));
这是我下载文件设置的文件名!

无语了,一群人回帖不看帖的么,还是我的问题表达不清楚
我没有做任何地页面跳转传参行为,只是单纯地要读取服务器指定目录下的中文文件名!
现在file.getName()显示出来的是乱码,getByte()无论转成UTF-8,GBK,ISO-8898-1都没有任何效果

汗,ISO-8859-1写成ISO-8898-1了,没权限编辑,引用说明一下。 --------------------编程问答--------------------  new String(fileName.getBytes("iso-8859-1"), "gbk"));
或 new String(fileName.getBytes("iso-8859-1"), "utf-8"));
试试!
我本地读取文件的文件名没问题~ --------------------编程问答--------------------
引用 11 楼 huixiangdeqiji 的回复:
 new String(fileName.getBytes("iso-8859-1"), "gbk"));
或 new String(fileName.getBytes("iso-8859-1"), "utf-8"));
试试!
我本地读取文件的文件名没问题~

我这句话真是白写了:“getByte()无论转成UTF-8,GBK,ISO-8859-1都没有任何效果”

我本地也没有任何问题,就是服务器不行。 --------------------编程问答-------------------- 你试试配置一下tomcat里面的server.xml的Connector节点加上URIEncoding="utf-8" --------------------编程问答--------------------
引用 13 楼 huixiangdeqiji 的回复:
你试试配置一下tomcat里面的server.xml的Connector节点加上URIEncoding="utf-8"



<Service name="Catalina">
  <Connector port="9020" URIEncoding="UTF-8"/>

  <Connector port="9021" protocol="AJP/1.3" maxThreads="550" connectionTimeout="1800000" disableUploadTimeout="true" URIEncoding="UTF-8"/>
    <Engine defaultHost="localhost" name="Catalina" jvmRoute="test1" >
     <Host appBase="webapps" autoDeploy="false" name="localhost" unpackWARs="false" xmlNamespaceAware="false" xmlValidation="false">
     </Host>
  </Engine>
</Service>
--------------------编程问答-------------------- 好吧,问题解决了,只要改完系统字符集把TOMCAT重启就可以了。

因为一些原因导致之前一直不能重启TOMCAT,后来可以重启时把这茬给忘记了
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,