Skip to content

Commit 9a30d41

Browse files
authored
Merge pull request #1 from hellosix/dev
Dev
2 parents 4521a2d + 60b718c commit 9a30d41

File tree

9 files changed

+363
-28
lines changed

9 files changed

+363
-28
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
<version>2.4</version>
8585
<classifier>jdk15</classifier>
8686
</dependency>-->
87+
88+
<dependency>
89+
<groupId>org.projectlombok</groupId>
90+
<artifactId>lombok</artifactId>
91+
<version>1.18.2</version>
92+
<scope>provided</scope>
93+
</dependency>
8794
</dependencies>
8895

8996
<build>
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
package cn.hellosix.zookeeper.controller;
22

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;
37
import cn.hellosix.zookeeper.service.IZKService;
48
import com.alibaba.fastjson.JSONObject;
5-
import cn.hellosix.zookeeper.entity.ZKParam;
9+
import lombok.extern.slf4j.Slf4j;
610
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;
717
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;
927

1028
/**
1129
* Created by lzz on 17/5/7.
1230
*/
1331
@Controller
32+
@Slf4j
1433
public class IndexController {
1534

1635
@Autowired
1736
private IZKService zkService;
1837

38+
@Autowired
39+
private IFileService fileService;
40+
1941
@RequestMapping("/")
2042
public String treeAdmin() {
2143
return "index";
2244
}
2345

2446

25-
@RequestMapping(value="/getAllPath", method = RequestMethod.GET)
47+
@RequestMapping(value = "/getAllPath", method = RequestMethod.GET)
2648
@ResponseBody
2749
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)) {
3052
return new JSONObject();
3153
}
3254
ZKParam param = new ZKParam();
@@ -36,11 +58,11 @@ public JSONObject getAllPath(@RequestParam String address,
3658
return jsonObject;
3759
}
3860

39-
@RequestMapping(value="/getPathDetail", method = RequestMethod.GET)
61+
@RequestMapping(value = "/getPathDetail", method = RequestMethod.GET)
4062
@ResponseBody
4163
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)) {
4466
return new JSONObject();
4567
}
4668
ZKParam param = new ZKParam();
@@ -51,27 +73,52 @@ public JSONObject getPathDetail(@RequestParam String address,
5173
return nodeDetail;
5274
}
5375

54-
@RequestMapping(value="/updateNode", method = RequestMethod.POST)
76+
@RequestMapping(value = "/updateNode", method = RequestMethod.POST)
5577
@ResponseBody
56-
public JSONObject updatePathData(@RequestBody ZKParam zkParam){
78+
public JSONObject updatePathData(@RequestBody ZKParam zkParam) {
5779
JSONObject nodeDetail = new JSONObject();
5880
try {
5981
nodeDetail = zkService.updateNodeData(zkParam);
6082
} catch (Exception e) {
61-
nodeDetail.put("data", e.getMessage() );
83+
nodeDetail.put("data", e.getMessage());
6284
}
6385
return nodeDetail;
6486
}
6587

66-
@RequestMapping(value="/deleteMode", method = RequestMethod.POST)
88+
@RequestMapping(value = "/deleteMode", method = RequestMethod.POST)
6789
@ResponseBody
68-
public boolean deleteNode(@RequestBody ZKParam zkParam){
90+
public boolean deleteNode(@RequestBody ZKParam zkParam) {
6991
boolean res = false;
7092
try {
71-
res = zkService.removeNode(zkParam);
93+
res = zkService.removeNode(zkParam);
7294
} catch (Exception e) {
7395
res = false;
7496
}
7597
return res;
7698
}
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+
}
77124
}

src/main/java/cn/hellosix/zookeeper/entity/Constants.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
* @date: 2018/8/29
66
*/
77
public class Constants {
8-
private Constants(){}
8+
/**
9+
* 暂存文件名称前缀
10+
*/
11+
public static final String TEMP_FILE_PREFIX = "download_";
912

1013
public static final String STAT = "stat";
1114

@@ -20,4 +23,31 @@ private Constants(){}
2023
public static final String TEMP_LEAF = "tempLeaf";
2124

2225
public static final String LEAF = "leaf";
26+
/**
27+
* 暂存文件名称后缀
28+
*/
29+
public static final String TEMP_FILE_SUFFIX = ".tmp";
30+
/**
31+
* 查询参数path前缀
32+
*/
33+
public static final String QUERY_PARAM_PATH_PREFIX = "/";
34+
/**
35+
* 内容布置的类型
36+
*/
37+
public static final String CONTENT_DISPOSITION_TYPE = "attachment";
38+
/**
39+
* 暂存文件超时分钟数(10分钟)
40+
*/
41+
public static final long TEMP_FILE_EXPIRE_TIME_IN_MINUTES = 10;
42+
/**
43+
* 暂存文件超时毫秒数
44+
*/
45+
public static final long TEMP_FILE_EXPIRE_TIME_IN_MILLISECONDS = TEMP_FILE_EXPIRE_TIME_IN_MINUTES * 60 * 1000L;
46+
/**
47+
* 内容类型,应用八进制流
48+
*/
49+
public static final String CONTENT_TYPE_APPLICATION_OCTET_STREAM = "application/octet-stream";
50+
51+
private Constants() {
52+
}
2353
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.hellosix.zookeeper.entity;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
import java.nio.file.Path;
7+
8+
/**
9+
* 文件数据传输对象
10+
*
11+
* @author Zenuo
12+
* @date 2018/09/22
13+
*/
14+
@Builder
15+
@Getter
16+
public final class FileDTO {
17+
/**
18+
* 文件路径
19+
*/
20+
private Path path;
21+
22+
/**
23+
* 文件名称
24+
*/
25+
private String name;
26+
}

src/main/java/cn/hellosix/zookeeper/service/FileService.java

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cn.hellosix.zookeeper.service;
2+
3+
import cn.hellosix.zookeeper.entity.FileDTO;
4+
import cn.hellosix.zookeeper.entity.ZKParam;
5+
6+
/**
7+
* 下载、上传文件
8+
*
9+
* @author Jay.H.Zou, Zenuo
10+
* @date 2018/8/26
11+
*/
12+
public interface IFileService {
13+
14+
/**
15+
* 根据指定的path下载对应的node数据
16+
*
17+
* @param param 参数实例
18+
* @param recursive 是否递归下载子节点
19+
* @return 文件数据传输对象实例
20+
* @throws Exception 异常
21+
*/
22+
FileDTO download(ZKParam param, Boolean recursive) throws Exception;
23+
}

0 commit comments

Comments
 (0)