Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java read linux file name garbled how to solve

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Java read linux file name garbled how to solve? Many people do not understand, today, in order to let you know more about the solution of Java reading linux file name garbled, so give you a summary of the following content, let's look down together.

The solution of Java Reading linux system File name and garbled Code

1, problem description

The web application wants to read the linux system file through Java and display it on the web page, but the Chinese file name is garbled?

Problem scenario description: when a user accesses the tomcat server through a browser and requests to view the file list information under a certain path, the web application of tomcat calls the getFilesByPath (String path) method to read the file list information in response to the request, and the file name in the page is garbled.

The code is as follows:

File class

Import java.io.Serializable;import java.util.Date;public class FileInfo implements Serializable {/ * / private static final long serialVersionUID = 1L X 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;}}

The java code is as follows:

/ * * find the list of visible files under the path path * @ param path* @ return*/public List getFilesByPath (String path) {List fileList = new ArrayList (); File file = new File (path); / * * find the list of visual files * / File [] files = file.listFiles (new FileFilter () {@ Overridepublic boolean accept (File f) {return! f.isHidden (); / / filter hidden files}}) / * java file type conversion to web display file class * / 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;}

When the read file information is displayed on the web page, the Chinese file name is garbled.

2, solution

After the web system calls this method to read the list of files, the files or folders named in Chinese are displayed as garbled. Data stream coding is often caused by inconsistent, check the data stream coding, first check the tomcat server and page display code is utf-8 coding, in the view of the linux system code for iso-8859-15, of course, we also know that java default is Unicode encoding, but this default Unicode coding is not what we want utf-8 coding, so we need to transcode.

Solution 1:

Change the linux system code to utf-8, which is relatively simple, but requires the user to have system root permission before it can be modified.

Solution 2:

The system code is converted to utf-8 code by java program transcoding.

/ * * find the list of visible files under the path path * @ param path* @ return*/public List getFilesByPath (String path) {/ / get the system code String encoding = System.getProperty ("file.encoding"); List fileList = new ArrayList (); File file = new File (path); / * * find the visual file * / File [] files = file.listFiles (new FileFilter () {@ Overridepublic boolean accept (File f) {return! f.isHidden ();}}) / * java file type converted to web display file type * / for (File f: files) {FileInfo fileInfo = new FileInfo (); try {/ / convert system encoding encoding to utf-8 encoding 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;}

The page shows that the file name is normal, but the path parameter appears garbled in Chinese when traversing the file, resulting in the path does not exist.

The solution is to convert the path parameter from utf-8 to system code, and then traverse the file according to path.

/ * find the list of visible files under the path path * @ param path* @ return*/public List getFilesByPath (String path) throws Exception {/ / get the system code String encoding = System.getProperty ("file.encoding"); List fileList = new ArrayList (); try {/ / transcode the path path to the system code File file = new File (new String (path.getBytes ("UTF-8"), encoding)) / / determine whether the file path exists and whether it has read permission. If it does not exist or does not have permission, throw an exception if (file.exists () & & file.isDirectory () & & file.canRead ()) {/ * find the visual file * / File [] files = file.listFiles (new FileFilter () {@ Overridepublic boolean accept (File f) {return! f.isHidden ();}})) / * convert java file type to web display file type * / for (File f: files) {FileInfo fileInfo = new FileInfo (); / / convert system encoding encoding to utf-8 encoding 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;}

In this way, the problem of garbled file names in java reading linux system can be solved.

To solve the problem of garbled code is to trace it back to the source, and it is as simple as tangling coding all the way.

On Java reading linux file name garbled should be shared here, of course, not only the above and everyone's analysis methods, but the editor can ensure its accuracy is absolutely no problem. I hope that the above content can have a certain reference value for everyone, and can be put into practice. If you like this article, you might as well share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report