1
1
package com .zheng .common .util ;
2
2
3
+ import org .apache .commons .lang .StringUtils ;
3
4
import org .apache .velocity .Template ;
4
5
import org .apache .velocity .VelocityContext ;
5
6
import org .apache .velocity .app .Velocity ;
6
7
import org .apache .velocity .app .VelocityEngine ;
7
8
8
9
import java .io .File ;
9
10
import java .io .FileWriter ;
11
+ import java .util .Properties ;
10
12
11
13
/**
12
14
* Velocity工具类
@@ -23,9 +25,11 @@ public class VelocityUtil {
23
25
*/
24
26
public static void generate (String inputVmFilePath , String outputFilePath , VelocityContext context ) throws Exception {
25
27
try {
26
- Velocity .init ();
27
- VelocityEngine engine = new VelocityEngine ();
28
- Template template = engine .getTemplate (inputVmFilePath , "utf-8" );
28
+ Properties properties = new Properties ();
29
+ properties .setProperty (VelocityEngine .FILE_RESOURCE_LOADER_PATH , getPath (inputVmFilePath ));
30
+ Velocity .init (properties );
31
+ //VelocityEngine engine = new VelocityEngine();
32
+ Template template = Velocity .getTemplate (getFile (inputVmFilePath ), "utf-8" );
29
33
File outputFile = new File (outputFilePath );
30
34
FileWriter writer = new FileWriter (outputFile );
31
35
template .merge (context , writer );
@@ -35,4 +39,30 @@ public static void generate(String inputVmFilePath, String outputFilePath, Veloc
35
39
}
36
40
}
37
41
42
+ /**
43
+ * 根据文件绝对路径获取目录
44
+ * @param filePath
45
+ * @return
46
+ */
47
+ public static String getPath (String filePath ) {
48
+ String path = "" ;
49
+ if (StringUtils .isNotBlank (filePath )) {
50
+ path = filePath .substring (0 , filePath .lastIndexOf ("/" ) + 1 );
51
+ }
52
+ return path ;
53
+ }
54
+
55
+ /**
56
+ * 根据文件绝对路径获取文件
57
+ * @param filePath
58
+ * @return
59
+ */
60
+ public static String getFile (String filePath ) {
61
+ String file = "" ;
62
+ if (StringUtils .isNotBlank (filePath )) {
63
+ file = filePath .substring (filePath .lastIndexOf ("/" ) + 1 );
64
+ }
65
+ return file ;
66
+ }
67
+
38
68
}
0 commit comments