Solve the problem of java web reading local JSON files and garbled code
Recently, I encountered a problem when developing a project. I successfully read the JSON file in the project using Google de gson on myecilpes, then publish the project to tomcat, and then use the same method to prompt "the system cannot find the specified path"
Put the file under Src / config /
After looking for many articles, I found that the problem should be that after publishing to the server, reading the relative path of the file can not find the file, so I can only use the absolute path to find the project test under Tomcat's webapps
Path is
After modifying the read path, you can read the JSON file. The following is the method to obtain the absolute path from a separate Java class. Because there are spaces in the folder, I will replace the ones in which the spaces are changed
However, the content read out in this way is garbled. The reason may be that the system coding format is inconsistent with that of the compiler, so I use stream to read the file
Then parse the obtained string in JSON format
In this way, you can get the correct jsonobject
The above article to solve the problem of java web reading local JSON files and garbled code is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.