ShowCtrl.java
7.69 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.example.demo;
import freemarker.template.utility.StringUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
/**
* Created by jack on 2018/4/18.
*/
@RestController
@RequestMapping(path = "/show")
public class ShowCtrl {
private static final Logger logger = LoggerFactory.getLogger(ShowCtrl.class);
// private static final String DEFAULT_DIR = "E:\\图片\\demo.txt";
private static final String DEFAULT_DIR = "/home/jack/makesample/zhurui/shoes/ali_similar.info";
@Autowired
ShowSer showSer;
@RequestMapping("/index")
public ModelAndView index() {
return new ModelAndView("index");
}
@GetMapping(path = "/load")
public ModelAndView loadPhotoDir(@RequestParam String curDir) {
ModelAndView mv = new ModelAndView("showImage");
showSer.loadDir(curDir);
mv.addObject("curDir",curDir);
mv.addObject("photoViewObj", showSer.fetchCurentPhotos(curDir, 0));
return mv;
}
//跳转至样本展示页面
@GetMapping(path = "/toSampleShow")
public ModelAndView toSampleShow() {
return new ModelAndView("showSamples");
}
/**
* 获取样本table 分页数据
* @param req
* @param sampleDir
* @return
*/
@RequestMapping ("/getSamplesByPage")
@ResponseBody
public PageListResp<SampleViewObj> getSamplesByPage(PageTableReq req,String sampleDir ) {
if(! showSer.samplesMap.containsKey(sampleDir)){
showSer.loadSampleInfo(sampleDir);//统计样本个数
}
Integer count = showSer.samplesMap.get(sampleDir);
if(count == null || count == 0){//加载文件失败
return PageListResp.newBuilder().iTotalDisplayRecords(0).iTotalRecords(0).aaData(new ArrayList()).build();
}
PageListResp.Builder pageListResp = PageListResp.newBuilder().iTotalDisplayRecords(count.intValue()).iTotalRecords(count.intValue());
pageListResp.aaData(showSer.fetchSamplesByPage(sampleDir, req.getIDisplayStart(), req.getIDisplayLength()));
return pageListResp.build();
}
@GetMapping(path = "/next")
public ModelAndView nextPhoto(@RequestParam String curDir) {
ModelAndView mv = new ModelAndView("showImage");
mv.addObject("curDir",curDir);
mv.addObject("photoViewObj", showSer.fetchCurentPhotos(curDir, 1));
return mv;
}
@GetMapping(path = "/prev")
public ModelAndView prevPhoto(@RequestParam String curDir) {
ModelAndView mv = new ModelAndView("showImage");
mv.addObject("curDir",curDir);
mv.addObject("photoViewObj", showSer.fetchPrevPhotos(curDir));
return mv;
}
@GetMapping(path = "/jump")
public ModelAndView jumpSknPhoto(@RequestParam("curDir") String curDir, @RequestParam String skn) {
ModelAndView mv = new ModelAndView("showImage");
mv.addObject("curDir",curDir);
mv.addObject("photoViewObj", showSer.fetchSknPhotos(curDir, skn));
return mv;
}
@GetMapping(path = "/read")
public Object readPhotoBytes(@RequestParam("curDir") String curDir, @RequestParam("skn") String skn, @RequestParam("order") String order) {
try {
return showSer.readPhotoBytes(curDir, skn, order);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@GetMapping(path = "/readAli")
public Object readPhotoBytes(@RequestParam("file") String filePath) {
try {
String s = URLDecoder.decode(filePath, "UTF-8");
logger.info("readPhotoBytes --文件路径filePath {}" , filePath );
return showSer.readPhotoBytes( s);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 更新update文件
* @param flag
* @return
*/
/* @GetMapping("/updateSampleInfo")
public String updateSampleInfo(boolean flag, int currentIndex, String sampleDir){
File file = new File(sampleDir + "/update.info");
if(!showSer.checkFile(file)){
return "创建文件失败!";
}
if( !showSer.modifyFile(file, currentIndex, flag)){
return "更新失败!";
}
return "";
}
*/
@GetMapping("/updateSamplesByBatch")
public String updateSamplesByBatch(boolean flag, String indexListStr, String sampleDir){
File file = new File(sampleDir + "/update.info");
if(!showSer.checkFile(file)){
return "创建文件失败!";
}
if( !showSer.modifyFile(file, indexListStr, flag)){
return "更新失败!";
}
return "";
}
@RequestMapping("/loadSingleImage")
public ModelAndView loadSingleImage(){
return new ModelAndView("loadSingleImage");
}
@RequestMapping("/updateImageFile")
@ResponseBody
public String updateImageFile(@RequestParam String mainPicSrc, @RequestParam String removeImageSrc, @RequestParam String curDir){
try {
//特殊字符--解码
mainPicSrc = URLDecoder.decode(mainPicSrc, "UTF-8");
removeImageSrc = URLDecoder.decode(removeImageSrc, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "解析字符串出错!";
}
String s[] = StringUtils.split(mainPicSrc, "&");
String skn = "";//skn名称
for(int i = 0 ; i<s.length; i++){
if(StringUtils.startsWith(s[i], "skn")){
skn = s[i].substring( s[i].indexOf("=") + 1);
}
}
File file = new File(curDir + "/" + skn + ".txt" );
if( !showSer.checkFile(file)){
return "创建文件失败!";
};//创建文件
String mainImageName = showSer.getImageNameBySrc( mainPicSrc, curDir, skn) ;//main image name
String removeImageName = showSer.getImageNameBySrc(removeImageSrc, curDir, skn) ;//remove image name
String content = mainImageName + "," + removeImageName;
if( !showSer.insertLine(file, content)){
return "写入文件失败!";
};//写入文件
return "";
}
//跳转至样本展示页面--样本 和 前 50个展示
@GetMapping(path = "/toSamples")
public ModelAndView toSamples() {
return new ModelAndView("showSampleAndSimilar");
}
/**
* 获取样本table 分页数据
* @param req
* @param sampleDir
* @return
*/
@RequestMapping ("/loadImagesFromFile")
@ResponseBody
public PageListResp<ImageAndSimilarResp> loadImagesFromFile(PageTableReq req,String sampleDir ) {
int count = 0;
PageListResp.Builder pageListResp = PageListResp.newBuilder().iTotalDisplayRecords(count).iTotalRecords(count);
if(StringUtils.isEmpty( sampleDir )){
sampleDir = DEFAULT_DIR;
}
File file = new File(sampleDir);
if(!file.exists()){
return null;
}
count = showSer.countFileLines(file);//总行数
if( count > 0){//当前行的数据
pageListResp.iTotalDisplayRecords(count).iTotalRecords(count);
pageListResp.aaData(showSer.getImagesPyPage(file, req.getIDisplayStart(), req.getIDisplayLength()));
}else{
pageListResp.aaData(new ArrayList());
}
return pageListResp.build();
}
}