13693261870
昨天 542a030a93de7dbc39e185d4ce355c5894e3cd5b
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
package com.terra.system.service.all;
 
import com.terra.common.entity.all.StaticData;
import com.terra.common.helper.FileHelper;
import com.terra.common.helper.StringHelper;
import com.terra.common.helper.WebHelper;
import com.terra.system.entity.data.DirEntity;
import com.terra.system.entity.data.MetaFileEntity;
import com.terra.system.entity.data.VerEntity;
import com.terra.system.entity.sys.UserEntity;
import com.terra.system.helper.*;
import com.terra.system.service.data.DirService;
import com.terra.system.service.sys.TokenService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.*;
 
/**
 * 父上传服务
 * @author WWW
 */
@Service
public class BaseUploadService {
    @Resource
    protected DirService dirService;
 
    @Resource
    protected PathHelper pathHelper;
 
    @Resource
    protected TokenService tokenService;
 
    public final Log log = LogFactory.getLog(getClass());
 
    /**
     * 查询目录
     */
    public String selectPath() {
        return pathHelper.getTempPathName();
    }
 
    /**
     * 上传文件
     */
    public <T> List<MetaFileEntity> uploadData(T t, String path, boolean isGetGuid, HttpServletRequest req, HttpServletResponse res) throws Exception {
        StandardMultipartHttpServletRequest request = (StandardMultipartHttpServletRequest) req;
        req.setCharacterEncoding("utf-8");
        res.setContentType("application/json;charset=utf-8");
 
        if (t != null) {
            setEntity(t, request);
        }
 
        return getFiles(path, isGetGuid, request);
    }
 
    /**
     * 设置实体类
     */
    private <T> void setEntity(T t, StandardMultipartHttpServletRequest req) {
        Enumeration<String> enumeration = req.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
 
            try {
                Field field = t.getClass().getDeclaredField(key);
                field.setAccessible(true);
                String value = req.getParameter(key);
 
                switch (field.getType().toString()) {
                    case "double":
                        field.set(t, Double.valueOf(value));
                        break;
                    case "long":
                        field.set(t, Long.valueOf(value));
                        break;
                    case "int":
                        field.set(t, Integer.valueOf(value));
                        break;
                    case "class java.sql.Timestamp":
                        field.set(t, Timestamp.valueOf(value));
                        break;
                    //case "class java.lang.String":
                    default:
                        field.set(t, value);
                        break;
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
    }
 
    /**
     * 获取文件
     */
    public List<MetaFileEntity> getFiles(String subPath, boolean isGetGuid, StandardMultipartHttpServletRequest req) throws Exception {
        List<MetaFileEntity> list = new ArrayList<>();
 
        String path = pathHelper.getTempPath(subPath);
        Iterator<String> iterator = req.getFileNames();
        while (iterator.hasNext()) {
            MultipartFile file = req.getFile(iterator.next());
            if (StringHelper.isEmpty(file.getOriginalFilename())) {
                continue;
            }
 
            MetaFileEntity mf = new MetaFileEntity();
            mf.setName(file.getOriginalFilename());
 
            double sizes = FileHelper.sizeToMb(file.getSize());
            mf.setSizes(sizes);
            mf.setPath(path + File.separator + mf.getName());
 
            file.transferTo(new File(mf.getPath()));
            if (isGetGuid) {
                mf.setGuid(FileHelper.getFileMd5(mf.getPath()));
            }
 
            list.add(mf);
        }
 
        return list;
    }
 
    /**
     * 查询文件
     */
    public List<MetaFileEntity> selectFiles(String subPath, boolean hasMd5) {
        String root = pathHelper.getConfig().getTempPath() + File.separator + subPath;
 
        File file = new File(root);
        if (!file.exists() && !file.isDirectory()) {
            return null;
        }
        File[] files = file.listFiles();
        if (null == files || files.length == 0) {
            return null;
        }
 
        List<MetaFileEntity> list = new ArrayList<>();
        for (File f : files) {
            String fileName = FileHelper.getFileName(f.getPath());
            String extName = FileHelper.getExtension(fileName);
            if (!isFileValid(f.getName())) {
                continue;
            }
 
            MetaFileEntity mf = new MetaFileEntity();
            mf.setName(fileName);
            mf.setExtName(extName);
            mf.setSizes(FileHelper.sizeToMb(f.length()));
            mf.setPath(subPath + File.separator + fileName);
            if (hasMd5 && !StaticData.ZIP.equals(extName)) {
                mf.setGuid(FileHelper.getFileMd5(f.getPath()));
            }
 
            list.add(mf);
        }
 
        return list;
    }
 
    /**
     * 删除文件
     */
    public Integer deleteFiles(List<MetaFileEntity> list) {
        String root = pathHelper.getConfig().getTempPath();
 
        int count = 0;
        for (MetaFileEntity entity : list) {
            if (!StringHelper.isEmpty(entity.getPath())) {
                String file = root + File.separator + entity.getPath();
 
                File f = new File(file);
                if (f.exists()) {
                    f.delete();
                    count++;
                }
            }
        }
 
        return count;
    }
 
    /**
     * 查询映射
     */
    public List<MetaFileEntity> selectMappers(UserEntity ue, String path, DirEntity dir, VerEntity ver, String epsgCode) {
        List<MetaFileEntity> metas = selectFiles(path, true);
        if (null == metas || metas.isEmpty()) {
            return null;
        }
 
        List<MetaFileEntity> list = new ArrayList<>();
        for (MetaFileEntity meta : metas) {
            meta.setEventid(StringHelper.getGuid());
            meta.setDepcode(ue.getDepcode());
            meta.setVerid(ver.getId());
            meta.setCreateUser(ue.getId());
            meta.setEpsgCode(epsgCode);
 
            if (StaticData.ZIP.equals(meta.getExtName())) {
                List<MetaFileEntity> subs = getMapperFiles(path, meta);
                if (null != subs && subs.size() > 0) {
                    list.addAll(subs);
                    continue;
                }
            }
            list.add(meta);
        }
        setMetaType(list);
        setDircode(list, dir);
 
        return list;
    }
 
    /**
     * 获取映射文件
     */
    private List<MetaFileEntity> getMapperFiles(String path, MetaFileEntity meta) {
        String tempPath = pathHelper.getConfig().getTempPath();
        String zipFile = tempPath + File.separator + meta.getPath();
        File file = new File(zipFile);
        if (!file.exists() || file.isDirectory()) {
            return null;
        }
 
        File zipFolder = new File(pathHelper.getConfig().getTempPath() + File.separator + path + "_zip");
        if (!zipFolder.exists() || !zipFolder.isDirectory()) {
            zipFolder.mkdirs();
        }
 
        // String subPath = zipFolder + File.separator + meta.getName().toLowerCase().replace(".zip", "") .replace("(","").replace(")","").replace(" ","")
        String subPath = zipFolder.getPath();
        ZipHelper.unzip(zipFile, subPath);
 
        List<File> files = new ArrayList<>();
        getFilesByPath(files, subPath);
 
        return getMapperFiles(files, meta, tempPath.length() + 1);
    }
 
    /**
     * 根据路径获取文件
     */
    private void getFilesByPath(List<File> list, String path) {
        File file = new File(path);
        if (!file.isDirectory()) {
            if (isFileValid(file.getName())) {
                list.add(file);
            }
            return;
        }
 
        if (isGdbFile(file) || isOsgbFile(file)) {
            list.add(file);
            return;
        }
 
        File[] files = file.listFiles();
        if (null == files) {
            return;
        }
        for (File f : files) {
            if (f.isDirectory()) {
                getFilesByPath(list, f.getPath());
            } else {
                if (isFileValid(f.getName())) {
                    list.add(f);
                }
            }
        }
    }
 
    /**
     * 文件是/否有效
     */
    private boolean isFileValid(String fileName) {
        String extName = FileHelper.getExtension(fileName);
        if (fileName.startsWith(StaticData.TILDE) || !StaticData.ALL_EXTENSION.contains(extName)) {
            return false;
        }
 
        fileName = fileName.toLowerCase();
        for (String ext : StaticData.MAPPER_EXCLUDE_EXT) {
            if (fileName.contains(ext)) {
                return false;
            }
        }
 
        return true;
    }
 
    /**
     * 获取映射文件
     */
    private List<MetaFileEntity> getMapperFiles(List<File> files, MetaFileEntity meta, int start) {
        List<MetaFileEntity> list = new ArrayList<>();
        for (File f : files) {
            boolean isGdb = isGdbFile(f);
            boolean isOsgb = isOsgbFile(f);
            boolean isFile = isGdb || isOsgb;
            if (f.isDirectory() && !isFile) {
                continue;
            }
 
            String fileName = FileHelper.getFileName(f.getPath());
            String extName = isOsgb ? StaticData.OSGB : FileHelper.getExtension(fileName);
            if (!isFileValid(f.getName())) {
                continue;
            }
 
            if (isGdb) {
                List<MetaFileEntity> rs = getGdbMappers(f, meta, start);
                if (null != rs && rs.size() > 0) {
                    list.addAll(rs);
                }
                continue;
            }
            if (isOsgb) {
                list.add(getOsgbMapper(f, meta, start));
                continue;
            }
 
            MetaFileEntity mf = createMetaFileEntity(f, meta, fileName, extName);
            mf.setPath(f.getPath().substring(start));
            list.add(mf);
        }
 
        return list;
    }
 
    /**
     * 创建元数据文件实体类
     */
    private MetaFileEntity createMetaFileEntity(File f, MetaFileEntity meta, String fileName, String extName) {
        boolean isShp = StaticData.SHP.equals(extName);
 
        MetaFileEntity mf = createMetaFileEntity(meta);
        mf.setEventid(StringHelper.getGuid());
        mf.setName(fileName);
        mf.setExtName(extName);
 
        if (isShp) {
            List<String> shpFiles = getShpFiles(f.getPath());
            mf.setTab(fileName.replace(StaticData.SHP, ""));
            mf.setSizes(getFilesSize(shpFiles));
            mf.setGuid(getFilesMd5(shpFiles));
        } else {
            mf.setSizes(FileHelper.sizeToMb(f.length()));
            mf.setGuid(FileHelper.getFileMd5(f.getPath()));
        }
 
        return mf;
    }
 
    /**
     * 创建元数据文件实体类
     */
    private MetaFileEntity createMetaFileEntity(MetaFileEntity meta) {
        MetaFileEntity mf = new MetaFileEntity();
        mf.setDepcode(meta.getDepcode());
        mf.setVerid(meta.getVerid());
        mf.setCreateUser(meta.getCreateUser());
        mf.setEpsgCode(meta.getEpsgCode());
 
        return mf;
    }
 
    /**
     * 获取SHP文件集合
     */
    private List<String> getShpFiles(String shpPath) {
        List<String> list = new ArrayList<>();
        list.add(shpPath);
 
        for (int i = 0, c = StaticData.SHP_EXT.size(); i < c; i++) {
            String path = shpPath.replace(".shp", StaticData.SHP_EXT.get(i));
 
            File f = new File(path);
            if (f.exists() && !f.isDirectory()) {
                list.add(path);
            }
        }
 
        return list;
    }
 
    /**
     * 获取GDB文件集合
     */
    private List<String> getGdbFiles(String path) {
        List<String> list = new ArrayList<>();
 
        File[] files = new File(path).listFiles();
        if (null == files || files.length == 0) {
            return list;
        }
        for (File f : files) {
            list.add(f.getPath());
        }
 
        return list;
    }
 
    /**
     * 获取多文件的MD5
     */
    private String getFilesMd5(List<String> files) {
        List<String> list = new ArrayList<>();
        for (String file : files) {
            String md5 = FileHelper.getFileMd5(file);
            if (null != md5) {
                list.add(md5);
            }
        }
 
        if (list.size() > 0) {
            String str = StringHelper.join(list, ",");
            return FileHelper.getStringMd5(str);
        }
 
        return StringHelper.getGuid();
    }
 
    /**
     * 获取多文件的大小
     */
    private double getFilesSize(List<String> files) {
        long size = 0L;
        for (String file : files) {
            File f = new File(file);
            if (f.exists() && !f.isDirectory()) {
                size += f.length();
            }
        }
 
        return FileHelper.sizeToMb(size);
    }
 
    /**
     * 获取GDB文件映射
     */
    private List<MetaFileEntity> getGdbMappers(File f, MetaFileEntity meta, int start) {
        List<String> tabs = GdbHelper.getTabNames(f.getPath());
        if (null == tabs || tabs.size() == 0) {
            return null;
        }
 
        String fileName = FileHelper.getFileName(f.getPath());
        String extName = FileHelper.getExtension(fileName);
 
        List<String> gdbFiles = getGdbFiles(f.getPath());
        String md5 = getFilesMd5(gdbFiles);
        double sizes = getFilesSize(gdbFiles);
 
        List<MetaFileEntity> list = new ArrayList<>();
        for (String tab : tabs) {
            MetaFileEntity mf = createMetaFileEntity(meta);
            mf.setEventid(StringHelper.getGuid());
            mf.setName(fileName + "\\" + tab);
            mf.setExtName(extName);
            mf.setSizes(FileHelper.sizeToMb(f.length()));
            mf.setPath(f.getPath().substring(start));
            mf.setTab(tab);
            mf.setSizes(sizes);
            mf.setGuid(md5);
            list.add(mf);
        }
 
        return list;
    }
 
    /**
     * 是/否为GDB文件
     */
    private boolean isGdbFile(File f) {
        if (f.isDirectory() && f.getName().toLowerCase().endsWith(StaticData.GDB)) {
            File gdb = new File(f.getPath() + File.separator + "gdb");
 
            return gdb.exists() && !gdb.isDirectory();
        }
 
        return false;
    }
 
    /**
     * 设置元数据文件的类型
     */
    private void setMetaType(List<MetaFileEntity> list) {
        for (MetaFileEntity mf : list) {
            if (null != mf.getExtName()) {
                mf.setType(mf.getExtName().replace(".", ""));
            }
        }
    }
 
    /**
     * 处理目录
     */
    public void copePath(List<MetaFileEntity> list) {
        String basePath = pathHelper.getConfig().getTempPath() + File.separator;
        for (MetaFileEntity mf : list) {
            mf.setPath(mf.getPath().replace(basePath, ""));
        }
    }
 
    /**
     * 是/否为OSGB文件
     */
    private boolean isOsgbFile(File f) {
        if (f.isDirectory()) {
            File meta = new File(f.getPath() + File.separator + "metadata.xml");
            File data = new File(f.getPath() + File.separator + "Data");
 
            return meta.exists() && !meta.isDirectory() && data.exists() && data.isDirectory();
        }
 
        return false;
    }
 
    /**
     * 获取OSGB文件映射
     */
    private MetaFileEntity getOsgbMapper(File f, MetaFileEntity meta, int start) {
        String fileName = FileHelper.getFileName(f.getPath());
 
        List<String> files = new ArrayList<>();
        FileHelper.getFilesByPath(files, f.getPath());
 
        String md5 = getFilesMd5(files);
        double sizes = getFilesSize(files);
 
        MetaFileEntity mf = createMetaFileEntity(meta);
        mf.setEventid(StringHelper.getGuid());
        mf.setName(fileName);
        mf.setExtName(StaticData.OSGB);
        mf.setSizes(FileHelper.sizeToMb(f.length()));
        mf.setPath(f.getPath().substring(start));
        mf.setSizes(sizes);
        mf.setGuid(md5);
 
        return mf;
    }
 
    /**
     * 设置目录编码
     */
    private void setDircode(List<MetaFileEntity> list, DirEntity dir) {
        List<DirEntity> dirs = dirService.selectRecursiveById(dir.getId());
        for (MetaFileEntity mfe : list) {
            String filePath = mfe.getPath().toLowerCase().replace("/", "\\");
            String fileName = mfe.getName().toLowerCase();
 
            String code = findDirByPath(dirs, filePath, fileName);
            if (StringHelper.isEmpty(code)) {
                DirEntity baseDir = findBaseDirByPath(dirs, filePath);
                if (null != baseDir) {
                    createDirByPath(baseDir, mfe.getPath().replace("/", "\\"));
                    dirs = dirService.selectRecursiveById(dir.getId());
                    code = findDirByPath(dirs, filePath, fileName);
                }
            }
            code = StringHelper.isEmpty(code) ? dir.getCode() : code;
            mfe.setDircode(code);
        }
    }
 
    /**
     * 根据路径查找目录
     */
    private String findDirByPath(List<DirEntity> dirs, String filePath,String fileName) {
        for (DirEntity de : dirs) {
            if (filePath.contains(de.getFullName().toLowerCase() + "\\" + fileName)) {
                return de.getCode();
            }
        }
 
        return null;
    }
 
    /**
     * 根据路径查找父目录
     */
    private DirEntity findBaseDirByPath(List<DirEntity> dirs, String filePath) {
        for (DirEntity de : dirs) {
            if (StringHelper.isEmpty(de.getCode()) || de.getCode().length() != StaticData.FOUR) {
                continue;
            }
            if (filePath.contains(de.getFullName().toLowerCase() + "\\")) {
                return de;
            }
        }
 
        return null;
    }
 
    /**
     * 根据路径创建目录
     */
    private void createDirByPath(DirEntity baseDir, String filePath) {
        int start = filePath.toLowerCase().indexOf(baseDir.getFullName().toLowerCase() + "\\") + baseDir.getFullName().length() + 1;
        int end = filePath.lastIndexOf("\\");
        if (end <= start) {
            return;
        }
        if (0 == baseDir.getLevel()) {
            baseDir.setLevel(2);
        }
 
        HttpServletRequest req = WebHelper.getRequest();
        UserEntity ue = tokenService.getCurrentUser(req);
 
        String[] strs = filePath.substring(start, end).split("\\\\");
        for (String str : strs) {
            DirEntity entity = dirService.selectDirByName(str, baseDir.getId());
            if (null == entity) {
                entity = new DirEntity();
                entity.setPid(baseDir.getId());
                entity.setName(str);
                entity.setOrderNum(dirService.selectMaxOrderNum());
                entity.setLevel(baseDir.getLevel() + 1);
                entity.setCreateUser(ue.getId());
 
                dirService.insert(entity);
                baseDir = dirService.selectDir(entity.getId());
            } else {
                baseDir = entity;
            }
        }
    }
 
    /**
     * 获取参数 *
     * Enumeration<String> headers = req.getHeaderNames();
     * Enumeration<String> attributes = req.getAttributeNames();
     */
    public Map<String, String> getParams(StandardMultipartHttpServletRequest req) {
        Map<String, String> map = new HashMap<>(3);
 
        Enumeration<String> enumeration = req.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = req.getParameter(key);
            map.put(key, value);
        }
 
        return map;
    }
}