1
1
package cn .hellosix .zookeeper .controller ;
2
2
3
+ import cn .hellosix .zookeeper .entity .Constants ;
4
+ import cn .hellosix .zookeeper .entity .FileDTO ;
5
+ import cn .hellosix .zookeeper .entity .ZKParam ;
6
+ import cn .hellosix .zookeeper .service .IFileService ;
3
7
import cn .hellosix .zookeeper .service .IZKService ;
4
8
import com .alibaba .fastjson .JSONObject ;
5
- import cn . hellosix . zookeeper . entity . ZKParam ;
9
+ import lombok . extern . slf4j . Slf4j ;
6
10
import org .springframework .beans .factory .annotation .Autowired ;
11
+ import org .springframework .core .io .InputStreamResource ;
12
+ import org .springframework .core .io .Resource ;
13
+ import org .springframework .http .ContentDisposition ;
14
+ import org .springframework .http .HttpHeaders ;
15
+ import org .springframework .http .HttpStatus ;
16
+ import org .springframework .http .ResponseEntity ;
7
17
import org .springframework .stereotype .Controller ;
8
- import org .springframework .web .bind .annotation .*;
18
+ import org .springframework .web .bind .annotation .GetMapping ;
19
+ import org .springframework .web .bind .annotation .RequestBody ;
20
+ import org .springframework .web .bind .annotation .RequestMapping ;
21
+ import org .springframework .web .bind .annotation .RequestMethod ;
22
+ import org .springframework .web .bind .annotation .RequestParam ;
23
+ import org .springframework .web .bind .annotation .ResponseBody ;
24
+
25
+ import java .io .FileInputStream ;
26
+ import java .nio .charset .StandardCharsets ;
9
27
10
28
/**
11
29
* Created by lzz on 17/5/7.
12
30
*/
13
31
@ Controller
32
+ @ Slf4j
14
33
public class IndexController {
15
34
16
35
@ Autowired
17
36
private IZKService zkService ;
18
37
38
+ @ Autowired
39
+ private IFileService fileService ;
40
+
19
41
@ RequestMapping ("/" )
20
42
public String treeAdmin () {
21
43
return "index" ;
22
44
}
23
45
24
46
25
- @ RequestMapping (value = "/getAllPath" , method = RequestMethod .GET )
47
+ @ RequestMapping (value = "/getAllPath" , method = RequestMethod .GET )
26
48
@ ResponseBody
27
49
public JSONObject getAllPath (@ RequestParam String address ,
28
- @ RequestParam (value = "path" , defaultValue = "/" ) String path ){
29
- if ( !path .startsWith ("/" ) ) {
50
+ @ RequestParam (value = "path" , defaultValue = "/" ) String path ) {
51
+ if ( !path .startsWith (Constants . QUERY_PARAM_PATH_PREFIX )) {
30
52
return new JSONObject ();
31
53
}
32
54
ZKParam param = new ZKParam ();
@@ -36,11 +58,11 @@ public JSONObject getAllPath(@RequestParam String address,
36
58
return jsonObject ;
37
59
}
38
60
39
- @ RequestMapping (value = "/getPathDetail" , method = RequestMethod .GET )
61
+ @ RequestMapping (value = "/getPathDetail" , method = RequestMethod .GET )
40
62
@ ResponseBody
41
63
public JSONObject getPathDetail (@ RequestParam String address ,
42
- @ RequestParam (value = "path" , defaultValue = "/" ) String path ){
43
- if ( !path .startsWith ("/" ) ) {
64
+ @ RequestParam (value = "path" , defaultValue = "/" ) String path ) {
65
+ if ( !path .startsWith (Constants . QUERY_PARAM_PATH_PREFIX )) {
44
66
return new JSONObject ();
45
67
}
46
68
ZKParam param = new ZKParam ();
@@ -51,27 +73,52 @@ public JSONObject getPathDetail(@RequestParam String address,
51
73
return nodeDetail ;
52
74
}
53
75
54
- @ RequestMapping (value = "/updateNode" , method = RequestMethod .POST )
76
+ @ RequestMapping (value = "/updateNode" , method = RequestMethod .POST )
55
77
@ ResponseBody
56
- public JSONObject updatePathData (@ RequestBody ZKParam zkParam ){
78
+ public JSONObject updatePathData (@ RequestBody ZKParam zkParam ) {
57
79
JSONObject nodeDetail = new JSONObject ();
58
80
try {
59
81
nodeDetail = zkService .updateNodeData (zkParam );
60
82
} catch (Exception e ) {
61
- nodeDetail .put ("data" , e .getMessage () );
83
+ nodeDetail .put ("data" , e .getMessage ());
62
84
}
63
85
return nodeDetail ;
64
86
}
65
87
66
- @ RequestMapping (value = "/deleteMode" , method = RequestMethod .POST )
88
+ @ RequestMapping (value = "/deleteMode" , method = RequestMethod .POST )
67
89
@ ResponseBody
68
- public boolean deleteNode (@ RequestBody ZKParam zkParam ){
90
+ public boolean deleteNode (@ RequestBody ZKParam zkParam ) {
69
91
boolean res = false ;
70
92
try {
71
- res = zkService .removeNode (zkParam );
93
+ res = zkService .removeNode (zkParam );
72
94
} catch (Exception e ) {
73
95
res = false ;
74
96
}
75
97
return res ;
76
98
}
99
+
100
+ @ GetMapping (value = "/download" )
101
+ public ResponseEntity <Resource > download (@ RequestParam String address ,
102
+ @ RequestParam (value = "path" , defaultValue = Constants .QUERY_PARAM_PATH_PREFIX ) String path ) {
103
+ //创建参数实例
104
+ final ZKParam param = new ZKParam ();
105
+ param .setZkAddress (address );
106
+ param .setZkPath (path );
107
+ try {
108
+ //调用下载服务
109
+ final FileDTO fileDTO = fileService .download (param , false );
110
+ //内容布置
111
+ final String contentDisposition = ContentDisposition .builder (Constants .CONTENT_DISPOSITION_TYPE )
112
+ .filename (fileDTO .getName ().replaceAll ("/" , "_" ), StandardCharsets .UTF_8 )
113
+ .build ().toString ();
114
+ //返回
115
+ return ResponseEntity .ok ()
116
+ .header (HttpHeaders .CONTENT_DISPOSITION , contentDisposition )
117
+ .header (HttpHeaders .CONTENT_TYPE , Constants .CONTENT_TYPE_APPLICATION_OCTET_STREAM )
118
+ .body (new InputStreamResource (new FileInputStream (fileDTO .getPath ().toFile ())));
119
+ } catch (Exception e ) {
120
+ log .error ("下载异常" , e );
121
+ return new ResponseEntity <>(HttpStatus .INTERNAL_SERVER_ERROR );
122
+ }
123
+ }
77
124
}
0 commit comments