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
package com.hmit.kernes.controller;
import com.hmit.kernes.entity.AppPostEntity;
import com.hmit.kernes.service.AppPostService;
import com.hmit.kernes.service.CloudBaseInfoService;
import com.hmit.kernes.utils.PageUtils;
import com.hmit.kernes.utils.Query;
import com.hmit.kernes.utils.R;
import com.hmit.kernes.utils.RRException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/app/post")
public class AppPostController {
@Autowired
AppPostService appPostService;
@Autowired
CloudBaseInfoService cloudBaseInfoService;
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
Query query = new Query(params);
List<AppPostEntity> list=appPostService.queryList(query);
int total=appPostService.queryTotal(query);
PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
return R.ok().put("page", pageUtil);
}
@RequestMapping("/info/{id}")
public R info(@PathVariable("") Long id){
AppPostEntity appPostEntity=appPostService.queryObject(id);
return R.ok().put("appPost",appPostEntity);
}
@RequestMapping("/save")
public R save(@RequestBody AppPostEntity appPostEntity){
//数据校验
verify(appPostEntity);
// System.out.println(appPostEntity.getImgSrc());
appPostEntity.setUnionName(cloudBaseInfoService.queryObject(appPostEntity.getUnionId()).getLocalUnionName());
appPostService.save(appPostEntity);
return R.ok();
}
@RequestMapping("/delete")
public R delete(@RequestBody Long[] ids){
appPostService.deleteBatch(ids);
return R.ok();
}
@RequestMapping("/update")
public R update(@RequestBody AppPostEntity appPostEntity){
//数据校验
verify(appPostEntity);
appPostService.update(appPostEntity);
return R.ok();
}
public AppPostEntity verify(AppPostEntity a){
if(StringUtils.isBlank(a.getMakerTel())){
throw new RRException("用户手机号不能为空");
}
a.setUpdateTime(new Date());
return a;
}
@ResponseBody
@RequestMapping("/uploadpho")
public R uploadpho(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
String url = request.getSession().getServletContext().getRealPath("doc/img/");
System.out.println(url);
String fileName2="";
if (file.isEmpty()) {
throw new RRException("上传文件不能为空");
}
try{
url = url.replaceAll("/", "\\\\");
String fileName = file.getOriginalFilename();
// fileName=tel+fileName;
fileName2=fileName;
File file3 = new File(url);
if ((!file3.exists()) && (!file3.isDirectory())) {
file3.mkdir();
}
File file2 = new File(file3, fileName);
InputStream in = file.getInputStream();
FileOutputStream fos = new FileOutputStream(file2);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int b = -1;
byte[] buffer = new byte[10485760];
while ((b = in.read(buffer)) != -1) {
bos.write(buffer, 0, b);
}
bos.flush();
bos.close();
fos.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return R.ok().put("fileName",fileName2);
}
@ResponseBody
@RequestMapping("/delpho")
public R delpho(@RequestBody String filename, HttpServletRequest request) throws Exception {
String url = request.getSession().getServletContext().getRealPath("doc/img/");
if (StringUtils.isBlank(filename)) {
return R.error().put("msg","删除目标文件名不能为空");
}
try{
url = url.replaceAll("/", "\\\\");
String fileName =filename;
File file4 = new File(url+"\\" +fileName);
// System.out.println(url+fileName);
if ((file4.isFile()) && (file4.exists())) {
file4.delete();
return R.ok();
}else {
return R.error().put("msg","目标文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
}
return R.ok();
}
}