本文摘自PHP中文网,作者藏色散人,侵删。
Java读linux系统文件文件名乱码的解决办法
1,问题描述
web应用想通过Java读取linux系统文件显示到web页面上,结果中文文件名出现乱码?
问题场景描述:当用户通过浏览器访问tomcat服务器,请求查看某一路径下文件列表信息,tomcat的web应用调用getFilesByPath(String path)方法读取文件列表信息响应请求,页面中文文件名出现乱码。
推荐教程:《java学习》
代码如下:
文件类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.io.Serializable;
import java.util. Date ;
public class FileInfo implements Serializable{
private static final long serialVersionUID = 1L;
String name;
long size;
Date lastModified;
boolean dir;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public Date getLastModified() {
return lastModified;
}
public void setLastModified( Date lastModified) {
this.lastModified = lastModified;
}
public boolean isDir() {
return dir;
}
public void setDir(boolean dir) {
this.dir = dir;
}
}
|
java代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public List<FileInfo> getFilesByPath(String path){
List<FileInfo> fileList = new ArrayList<FileInfo>();
File file = new File(path);
File[] files = file.listFiles( new FileFilter() {
@Override
public boolean accept(File f) {
return !f.isHidden();
}
});
for (File f : files) {
FileInfo fileInfo = new FileInfo();
fileInfo.setName(f.getName());
fileInfo.setSize(f.length());
fileInfo.setLastModified( new Date (f.lastModified()));
fileInfo.setDir(f.isDirectory());
}
return fileList;
}
|
当将读取文件信息显示到web页面上时,中文文件名出现乱码。
2,解决方案
web系统调用该方法读取文件列表后,中文命名的文件或文件夹显示为乱码。出现乱码往往都是数据流编码不一致导致的,检查数据流编码,首先查看tomcat服务器和页面显示编码都是utf-8编码,在查看linux系统编码为iso-8859-15,当然我们也知道java默认采用的是Unicode编码,但这种默认Unicode编码不是我们想要的utf-8编码,所以我们需要自己转码。
解决方案1:
将linux系统编码修改为utf-8,该方案比较简单,不过要求用户有系统root权限才可以修改。
解决方案2:
通过java程序转码,将系统编码转换为utf-8编码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public List<FileInfo> getFilesByPath(String path){
String encoding = System.getProperty( "file.encoding" );
List<FileInfo> fileList = new ArrayList<FileInfo>();
File file = new File(path);
File[] files = file.listFiles( new FileFilter() {
@Override
public boolean accept(File f) {
return !f.isHidden();
}
});
for (File f : files) {
FileInfo fileInfo = new FileInfo();
try {
fileInfo.setName( new String(f.getName().getBytes(encoding), "UTF-8" ));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
fileInfo.setSize(f.length());
fileInfo.setLastModified( new Date (f.lastModified()));
fileInfo.setDir(f.isDirectory());
}
return fileList;
}
|
页面显示文件名正常,但path参数在遍历文件的时候中文出现了乱码,导致路径不存在。
解决方法是将path参数从utf-8转换为系统编码,再根据path去遍历文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | public List<FileInfo> getFilesByPath(String path) throws Exception{
String encoding = System.getProperty( "file.encoding" );
List<FileInfo> fileList = new ArrayList<FileInfo>();
try {
File file = new File( new String(path.getBytes( "UTF-8" ),encoding));
if (file.exists() && file.isDirectory() && file.canRead()){
File[] files = file.listFiles( new FileFilter() {
@Override
public boolean accept(File f) {
return !f.isHidden();
}
});
for (File f : files) {
FileInfo fileInfo = new FileInfo();
fileInfo.setName( new String(f.getName().getBytes(encoding), "UTF-8" ));
fileInfo.setSize(f.length());
fileInfo.setLastModified( new Date (f.lastModified()));
fileInfo.setDir(f.isDirectory());
}
} else {
throw new Exception( "Paht:" +path+ ", exists:" +file.exists()+ ", canRead:" +file.isDirectory());
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
return fileList;
}
|
这样就可以解决java读linux系统文件名乱码问题。。。
解决乱码问题就是从源头追溯的显示,一路纠结编码就这么简单。。。
以上就是Java读linux文件名乱码的解决办法的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
linux下怎么运行Java?
linux下怎么运行Java?
linux学习第二篇之Java环境安装配置图文教程
centos如何查看是否安装了Java
Java读linux文件名乱码的解决办法
Java读linux文件名乱码的解决办法
更多相关阅读请进入《Java》频道 >>
转载请注明出处:木庄网络博客 » Java读linux文件名乱码的解决办法