一:java中的路径
1.直接使用相对路径
new File("src/main/resources/" + imageName)
在Linux系统下会出问题,慎用
2.使用ClassPathResource获取classes下文件路径(spring自带)
ClassPathResource cpr = new ClassPathResource("image"+File.separator+"logo.png");
InputStream in = cpr.getInputStream();
3.使用getClassLoader().getResource获取classes下文件路径(jdk自带)
URI inUri = this.getClass().getClassLoader().getResource("object_collection_template.xls").toURI();
InputStream in = Files.newInputStream(Paths.get(inUri));
4.使用System.getProperty(“user.dir”)获取工程root源路径
String root = System.getProperty("user.dir");
5.使用new ApplicationHome(class)
二:Json的使用
1.使用FastJson
String str = JSON.toJSONString(Object)
Object obj = JSON.parseObject(String, class)
JSONObject jsonObject = JSONObject.parseObject(String)
Stringstr = jsonObject.toJSONString()
注意:使用JSON.toJSONString时会默认调用类中的所有get方法
2.使用Gson
Gson gson = new Gson();
String str = gson.toJson(Object)
Object object = gson.fromJson(jsonString, Object.class)