FileController.java
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cn.com.glsx.base.modules.controller;
import com.glsx.plat.common.utils.SnowFlake;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.context.properties.UploadProperties;
import com.glsx.plat.core.web.R;
import com.glsx.plat.dfs.fastdfs.FastDfsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
/**
* @author payu
*/
@Slf4j
@RestController
@RequestMapping(value = "basic/file")
public class FileController {
@Resource
private FastDfsUtils fastDfsUtils;
@Resource
private UploadProperties uploadProperties;
/**
* 文件上传到本地服务器
*/
@PostMapping(value = "/upload2local", headers = "content-type=multipart/form-data")
public R upload2local(@RequestParam("file") MultipartFile file) throws Exception {
String result;
//获取文件存放路径
String path = uploadProperties.getBasePath();
File targetDir = new File(path);
if (!targetDir.exists()) {
targetDir.mkdirs();
}
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.indexOf("."));
String newFilename = SnowFlake.nextId() + suffix;
String tempPath = targetDir + File.separator + newFilename;
// 转存文件
File desc = new File(tempPath);
FileUtils.copyInputStreamToFile(file.getInputStream(), desc);
path = tempPath;
if (StringUtils.isNotEmpty(path)) {
result = uploadProperties.getFilepath() + newFilename;
return R.ok().data(result);
} else {
result = "上传失败";
return R.error(result);
}
}
/**
* 文件上传分布式文件服务器
*/
@PostMapping(value = "/upload2dfs", headers = "content-type=multipart/form-data")
public R upload2dfs(@RequestParam("file") MultipartFile file) {
String result;
try {
String path = fastDfsUtils.upload(file);
if (StringUtils.isNotEmpty(path)) {
result = path;
} else {
result = "上传失败";
}
return R.ok().data(result);
} catch (Exception e) {
e.printStackTrace();
result = "文件服务异常";
return R.error(result);
}
}
/**
* 文件删除
*
* @param url
* @return
*/
@GetMapping(value = "/deleteByUrl")
public R deleteByUrl(@RequestParam("url") String url) {
fastDfsUtils.delete(url);
return R.ok();
}
}