FileController.java
5.63 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package vion.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.File;
import vion.model.Inspect;
import vion.model.Store;
import vion.service.IFileSerrvice;
import vion.utils.ResultUtil;
import vion.vo.StoreVO;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class FileController {
@Autowired
private IFileSerrvice fileSerrvice;
@Value("${fileurl:}")
private String fileurl;
@GetMapping("/files")
@ResponseBody
public Object getFiles(@RequestParam(name = "storeId",required=false)Integer store_id,
@RequestParam(name = "sourceId",required=false)Integer source_id,
@RequestParam(name = "pageNum")Integer pageNum,
@RequestParam(name = "pageSize")Integer pageSize){
//pageNum当前页,pageSize每页条数
Page page= PageHelper.startPage(pageNum,pageSize);
QueryWrapper<File> wrapper=new QueryWrapper<>();
if(store_id!=null)
wrapper.eq("store_id",store_id);
if(source_id!=null)
wrapper.eq("source_id",source_id);
List<File> fileList=fileSerrvice.list(wrapper);
fileList.sort(Comparator.comparing(File::getId));
PageInfo pageInfo=new PageInfo<>(page);
pageInfo.setList(fileList);
if (pageInfo != null) {
return ResultUtil.success(pageInfo);
} else {
return ResultUtil.error(ResultEnum.SELECT_ERROR);
}
}
@PostMapping("/files")
@ResponseBody
public Map<String, Object> getFile(@RequestBody File file){
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
//添加文件附件
File tempFile=new File();
tempFile.setStoreId(file.getStoreId());
tempFile.setSourceId(file.getSourceId());
tempFile.setSourceType(file.getSourceType());//1项目、2工单预处理,3工单操作,4巡检
tempFile.setName(file.getName());
tempFile.setUrl(file.getUrl());
tempFile.setType(file.getType());
fileSerrvice.save(tempFile);
resultMap.put("msg_code", 200);
} catch (Exception e) {
e.printStackTrace();
resultMap.put("msg_code", 500);
}
return resultMap;
}
@GetMapping("/deleteFile")
@ResponseBody
public Map<String, Object> deleteFile(@RequestParam(name = "id")Integer id){
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
//解除文件关系
fileSerrvice.lambdaUpdate()
.setSql("id="+id)
.eq(File::getSourceId,-1)
.update();
resultMap.put("msg_code", 200);
} catch (Exception e) {
e.printStackTrace();
resultMap.put("msg_code", 500);
}
return resultMap;
}
@PostMapping("/upLoadFile")
public Map<String, Object> uploadFile(@RequestParam(name="file") MultipartFile file){
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
//上传url地址
String path=fileurl+"/"+file.getOriginalFilename();
java.io.File fileinfo=new java.io.File(path);
file.transferTo(fileinfo);
resultMap.put("msg_code", 200);
resultMap.put("fileUrl",path);
} catch (IOException e) {
e.printStackTrace();
resultMap.put("msg_code", 500);
resultMap.put("message",e.getMessage());
}
return resultMap;
}
@GetMapping("/downLoadFile")
public void downLoadFile(@RequestParam(name="fileurl")String fileurl,
@RequestParam(name="filename")String filename,
HttpServletResponse response){
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename="+filename);
java.io.File file=new java.io.File(fileurl);
try {
FileInputStream fis=new FileInputStream(file);
OutputStream os=response.getOutputStream();
byte[] buffer=new byte[1024];
int length;
while((length=fis.read(buffer))>0){
os.write(buffer,0,length);
}
fis.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public List<File> getFileList(Integer store_id,Integer source_id){
QueryWrapper<File> wrapper=new QueryWrapper<>();
if(store_id!=null)
wrapper.eq("store_id",store_id);
if(source_id!=null)
wrapper.eq("source_id",source_id);
List<File> fileList=fileSerrvice.list(wrapper);
return fileList;
}
}