2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
package com.landtool.lanbase.modules.res.controller;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.SecurityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.landtool.lanbase.common.annotation.LogAction;
import com.landtool.lanbase.common.utils.HttpOperateUtils;
import com.landtool.lanbase.config.SysTemPropertyConfig;
import com.landtool.lanbase.modules.api.utils.DeleteFileUtils;
import com.landtool.lanbase.modules.res.entity.Res_ExtDataSource;
import com.landtool.lanbase.modules.res.entity.Res_ExtFileSource;
import com.landtool.lanbase.modules.res.entity.Res_FileSource_Way;
import com.landtool.lanbase.modules.res.entity.Res_MainInfo;
import com.landtool.lanbase.modules.res.entity.Res_StorageConfig;
import com.landtool.lanbase.modules.res.entity.Res_Storage_Relation;
import com.landtool.lanbase.modules.res.entity.JSONModels.NcConfig;
import com.landtool.lanbase.modules.res.entity.JSONModels.NcMessage;
import com.landtool.lanbase.modules.res.entity.ViewModels.FileSourceList;
import com.landtool.lanbase.modules.res.service.ResExtDataSourceService;
import com.landtool.lanbase.modules.res.service.ResExtFileSourceService;
import com.landtool.lanbase.modules.res.service.ResFileSourceWayService;
import com.landtool.lanbase.modules.res.service.ResMainInfoService;
import com.landtool.lanbase.modules.res.service.ResStorageConfigService;
import com.landtool.lanbase.modules.res.service.ResStorageRelaTionService;
import com.landtool.lanbase.modules.sys.service.SysFieldvalueService;
 
import cn.hutool.json.JSONUtil;
 
/**
 * @Author: lizhao
 * @Date: 2018-03-06 14:49
 * @Description:11系统资源扩展(文档数据)(RES_EXTFILESOURCE)
 */
@Controller
@RequestMapping("/res")
public class ResExtFileSourceController {
    @Autowired
    private ResExtFileSourceService resExtFileSourceService;
 
    @Autowired
    private SysTemPropertyConfig sysConfig;
 
    @Autowired
    private ResMainInfoService resMainInfoService;
 
    @Autowired
    private ResFileSourceWayService resFileSourceWayService;
 
    @Autowired
    private SysFieldvalueService FieldUtils;
    
    @Autowired
    private ResStorageConfigService resStorageConfigService;
    
    @Autowired
    private ResStorageRelaTionService resStorageRelaTionService;
    
    @Autowired
    private ResExtDataSourceService resExtDataSourceService;
 
    private String wenjianjiaPath() {
        return sysConfig.getUploadPath() + "FileSource\\wenjianjia\\";
    }
 
    private String wenjianPath() {
        return sysConfig.getUploadPath() + "FileSource\\wenjian\\";
    }
 
    /**
     * 访问资源文档数据图层信息页面
     */
    @RequestMapping("/ResManage/ResRegister/ExtFileSource")
    public String ExtFileSource(int resMainInfoId, Model model) {
        String unit = "";
        Res_ExtFileSource resExtFileSource = resExtFileSourceService.selectByPrimaryKey(resMainInfoId);
        if (resExtFileSource != null) {
            model.addAttribute("Res_ExtFileSource", resExtFileSource);
            model.addAttribute("resMainInfoId", resMainInfoId);
            model.addAttribute("uploadPath", resExtFileSource.getServerurl());
            model.addAttribute("absolutePath", sysConfig.getUploadPath()+resExtFileSource.getServerurl());
            //判断是否有大小
            if (resExtFileSource.getFilesize() != null) {
                String unitConvert = resExtFileSource.getFilesize() < 1 ? "1" : (resExtFileSource.getFilesize() < 1024 ? "2" : (resExtFileSource.getFilesize() < 1024 * 1024 ? "3" : "4"));
                model.addAttribute("unitConvert", unitConvert);
                switch (unitConvert) {
                    case "1":
                        unit = "bytes";
                        break;
                    case "2":
                        unit = "KB";
                        break;
                    case "3":
                        unit = "MB";
                        break;
                    case "4":
                        unit = "GB";
                        break;
                }
            } else {
                String unitConvert = "5";
                model.addAttribute("unitConvert", unitConvert);
            }
            Res_MainInfo res_mainInfo = resMainInfoService.selectByPrimaryKey(resMainInfoId);
            model.addAttribute("resMainInfo", res_mainInfo);
 
            // 文件夹
            if (resExtFileSource.getSourcetype() != null && resExtFileSource.getSourcetype().equals("文件夹")) {
                if (resExtFileSource.getServerurl() != null && resExtFileSource.getStoragemode() != null && resExtFileSource.getStoragemode().equals("本地")) {
                    File file = new File(sysConfig.getUploadPath() + resExtFileSource.getServerurl());
                    if (file.isDirectory()) {
                        System.out.println(resExtFileSource.getServerurl());
                        String[] fileLists = file.list();
                        File[] files = file.listFiles();
                        List <FileSourceList> fileSourceLists = new ArrayList <>();
                        for (int i = 0; i < fileLists.length; i++) {
                            File file1 = new File(fileLists[i]);
                            File file2 = new File(files[i].getAbsolutePath());
                            String size = com.landtool.lanbase.common.utils.FileUtils.setSize(file2.length());
                            String path2 = wenjianjiaPath() + file1.getName(); // 文件映射路径
                            FileSourceList sourceList = new FileSourceList();
                            sourceList.setName(file1.getName());
                            sourceList.setPath(path2);
                            sourceList.setSize(size);
                            Res_Storage_Relation res_Storage_Relation = new Res_Storage_Relation();
                            res_Storage_Relation.setResourceid(resMainInfoId);
                            res_Storage_Relation.setFilename(file1.getName());
                            Res_Storage_Relation resStorageRelation = resStorageRelaTionService.selectByResourceidAndFileName(res_Storage_Relation);
                            if(resStorageRelation!=null && resStorageRelation.getIsstorage()==1){
                                sourceList.setStorageConfig("已入库");
                            }else{
                                sourceList.setStorageConfig("未入库");
                            }
                            
                            if(resStorageRelation!=null && resStorageRelation.getIsgeneration()==1){
                                sourceList.setNcjson("已生成");
                            }else{
                                sourceList.setNcjson("未生成");
                            }
                            fileSourceLists.add(sourceList);
                        }
                        model.addAttribute("filelist", fileSourceLists);
                    }
                }
            }
        } else {
            model.addAttribute("unitConvert", "5");
            unit = "KB";
            Res_ExtFileSource resExtFileSource1 = new Res_ExtFileSource();
            model.addAttribute("Res_ExtFileSource", resExtFileSource1);
            model.addAttribute("resMainInfo", new Res_MainInfo());
        }
 
        model.addAttribute("unit", unit);
        // 获取文档类型列表
        LinkedHashMap <String, String> filetypelist = FieldUtils.getFieldListByKey("FileType");
        model.addAttribute("filetypelist", filetypelist);
 
        if (SecurityUtils.getSubject().isPermitted("org_user_admin")) {
            //判断当前用户是否是管理员,是管理员或是未提交的资源才可以修改资源相关信息
            model.addAttribute("admin", true);
        } else {
            model.addAttribute("admin", false);
        }
        model.addAttribute("ncSyncStorageUrl", sysConfig.getNcStorageUrl().substring(0, sysConfig.getNcStorageUrl().length()-9)+"jobs/");
        model.addAttribute("readNcFileURL", sysConfig.getReadNcFileURL());
        return "ResManage/ResRegister/ExtFileSource";
    }
 
    /**
     * 数据文件下的 展现方式
     */
    @RequestMapping("/ResManage/ResRegister/ExtPresentationMode")
    public String ExtPresentationMode(int resMainInfoId, Model model) {
        model.addAttribute("resMainInfoId", resMainInfoId);
        List <Res_FileSource_Way> list = resFileSourceWayService.selectById(resMainInfoId);
        model.addAttribute("FileSourceWay", list);
        return "ResManage/ResRegister/ExtPresentationMode";
    }
 
    /**
     * 添加或者更新 数据文件展现方式
     */
    @ResponseBody
    @RequestMapping("/ResManage/ResRegister/insertSelectiveAndUpdate")
    @LogAction("资源管理,资源发布,资源修改,修改|zy")
    public int insertSelectiveAndUpdate(Res_MainInfo resMainInfo, String extMapUrlStr) {
        // 删除id 对应的所有文件展示方式
        try {
            if (resMainInfo.getResourceid() != null) {
                resFileSourceWayService.deleteByPrimaryKey(resMainInfo.getResourceid());
            }
            if (!extMapUrlStr.equals("")) {
                String[] extMapUrlArray = extMapUrlStr.split("\\|");
                // 插入资源支持协议与地址表
                for (int i = 0; i < extMapUrlArray.length; i++) {
                    String[] extMapUrlArray2 = extMapUrlArray[i].split(",");
                    String Showway = extMapUrlArray2[0];
                    String url = extMapUrlArray2[1];
                    String remark = extMapUrlArray2[2];
                    Res_FileSource_Way resFileSourceWay = new Res_FileSource_Way();
                    resFileSourceWay.setShowway(Showway);
                    resFileSourceWay.setUrl(url);
                    resFileSourceWay.setRemark(remark);
                    resFileSourceWay.setResourceid(resMainInfo.getResourceid());
                    resFileSourceWay.setCreatedate(new Date());
                    resFileSourceWayService.insertSelective(resFileSourceWay);
                }
            }
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }
 
 
    /**
     * 根据id 删除文档数据列表功能
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/deleteByPrimaryKey")
    public int deleteByPrimaryKey(int resourceid) {
 
        // 删除数据库信息先把文件删除
        Res_ExtFileSource res_extFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid);
        String path = res_extFileSource.getServerurl();
        if (res_extFileSource.getSourcetype().equals("文件夹")) {
            //alert by ykm 2019/08/30 有的路径已经包含了文件夹还是文件目录
            if(path.indexOf("wenjianjia") == -1) {
                path = wenjianjiaPath() + path + "/";
            }
            else {
                path = sysConfig.getUploadPath() + path + "/";
            }
        }
        if (res_extFileSource.getSourcetype().equals("文件")) {
            //alert by ykm 2019/08/30 有的路径已经包含了文件夹还是文件目录
            if (path.indexOf("wenjian") == -1) {
                path = wenjianPath() + path;
            }
            else {
                path = sysConfig.getUploadPath() + path;
            }
        }
        DeleteFileUtils.deletefileUtils(path);
        return resExtFileSourceService.deleteByPrimaryKey(resourceid);
    }
 
    /**
     * 插入文档数据列表功能(所有元素不能为空)
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/insert")
    public int insert(Res_ExtFileSource record) {
        return resExtFileSourceService.insert(record);
    }
 
    /**
     * 插入文档数据列表功能(可以只填写必填字段)
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/insertSelective")
    public int insertSelective(Res_ExtFileSource record) {
        return resExtFileSourceService.insertSelective(record);
    }
 
    /**
     * 插入文档数据列表功能(可以只填写必填字段)
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/insertSelectiveAndUpdate")
    public int insertSelectiveAndUpdate(Res_ExtFileSource record, double savedsizenum) {
        record.setFilesize(savedsizenum);
        record.setServerurl(StringEscapeUtils.unescapeHtml(record.getServerurl()));
        Res_ExtFileSource resExtFileSource = resExtFileSourceService.selectByPrimaryKey(record.getResourceid());
        if (resExtFileSource == null) {
            // 添加
            if (record.getDownloadmodel().equals("FTP")) { // 把不属于FTP的字段设置为空
                record.setGettype(null);
            } else if (record.getDownloadmodel().equals("HTTP")) {
                record.setFtpusername(null);
                record.setFtppwd(null);
            }
            if (record.getSourcetype().equals("文件")) {
                TiHuanLuJin(record.getServerurl());
                record.setServerurl(record.getServerurl().replace("temp/", ""));
            }
            return resExtFileSourceService.insertSelective(record);
        } else {
            // 更新
            if (record.getSourcetype().equals("文件")) {
                TiHuanLuJin(record.getServerurl());
                if (!record.getServerurl().equals("") && !resExtFileSource.getServerurl().equals("") && resExtFileSource.getStoragemode().equals("本地")) {
                    String path = resExtFileSource.getServerurl();
                    path = sysConfig.getUploadPath() + path;
                    File file = new File(path);
                    if (file.exists()) {
                        DelectEquealFile(record.getServerurl(), resExtFileSource.getServerurl());
                    }
                }
                record.setServerurl(record.getServerurl().replace("temp/", ""));
            }
            if (record.getDownloadmodel().equals("FTP")) {
                record.setGettype(null);
            } else if (record.getDownloadmodel().equals("HTTP")) {
                record.setFtpusername(null);
                record.setFtppwd(null);
            }
            // 如果文件资源类型改变则把原有的文件删除
            if (!record.getSourcetype().equals(resExtFileSource.getSourcetype()) || record.getStoragemode().equals("远程")) {
                String path = resExtFileSource.getServerurl();
                if (resExtFileSource.getSourcetype().equals("文件夹") && resExtFileSource.getStoragemode() != null && resExtFileSource.getStoragemode().equals("本地")) {
                    path = sysConfig.getUploadPath() + path;
                    File file = new File(path);
                    if (file.exists()) {
                        DeleteFileUtils.deletefileUtils(path);
                    }
                }
                if (resExtFileSource.getSourcetype().equals("文件") && resExtFileSource.getStoragemode() != null && resExtFileSource.getStoragemode().equals("本地")) {
                    path = sysConfig.getUploadPath() + path;
                    File file = new File(path);
                    if (file.exists()) {
                        DeleteFileUtils.deletefileUtils(path);
                    }
                }
            }
            return resExtFileSourceService.updateByPrimaryKey(record);
        }
    }
 
    //删除相同文件
    private void DelectEquealFile(@RequestBody String NEWString, String OLDString) {
        String oldourl = OLDString.substring(OLDString.lastIndexOf("/"), OLDString.length());
        String newurl = NEWString.substring(NEWString.lastIndexOf("/"), NEWString.length());
        if (!oldourl.equals(newurl)) {
 
            File OLDFile = new File(sysConfig.getUploadPath() + OLDString.replace("/", "\\"));
            System.out.println(OLDFile);
            OLDFile.delete();
        }
    }
 
    //替换路径
    private void TiHuanLuJin(@RequestBody String lujin) {
        String oldFileUrl = sysConfig.getUploadPath() + lujin.replace("/", "\\");
        File oldFile = new File(oldFileUrl);
        String NewFileUrl = sysConfig.getUploadPath() + lujin.replace("temp", "").replace("/", "\\");
        NewFileUrl = NewFileUrl.substring(0, NewFileUrl.lastIndexOf("\\")) + "\\";
        File NewFile = new File(NewFileUrl);
        if (!NewFile.exists()) { // 当前地址不为空,判断该路径是否存在,不存在则创建新的文件夹
            File newfilePath = new File(NewFile + "\\"); // 创建对应的年月文件夹
            newfilePath.mkdirs();
        }
        com.landtool.lanbase.common.utils.FileUtils.moveTotherFolders(oldFileUrl, NewFileUrl);
    }
 
    /**
     * 根据id查询文档数据列表
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/selectByPrimaryKey")
    public Res_ExtFileSource selectByPrimaryKey(int resourceid) {
        return resExtFileSourceService.selectByPrimaryKey(resourceid);
    }
 
    /**
     * 根据id更新文档数据列表(更新部分数据)
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/updateByPrimaryKeySelective")
    public int updateByPrimaryKeySelective(Res_ExtFileSource record) {
        return resExtFileSourceService.updateByPrimaryKeySelective(record);
    }
 
    /**
     * 根据id更新文档数据列表(更新所有数据)
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/updateByPrimaryKey")
    public int updateByPrimaryKey(Res_ExtFileSource record) {
        return resExtFileSourceService.updateByPrimaryKey(record);
    }
 
    /** 文件上传 */
    /**
     * @param sourcetype 文件资源类型
     * @return resourceid 资源ID
     * @Description:
     * @author ykm
     * @date 2018/4/19
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/uploadfile")
    public String uploadfile(@RequestParam("myFile") MultipartFile[] file, String sourcetype, String resourceid) {
 
        String thisSize = ""; // 当前文件大小
        String Filename = null;// 文件名称
        List Filenames = new ArrayList();// 文件名称总和
        List finasizes = new ArrayList();
        String name = ""; // 文件全称(上传时候的文件名称)
        String res = "";
        long size = 0; // 文件大小
        long totalSize = 0; //批量上传文件大小和
        for (int i = 0; i < file.length; i++) {
            if (file[i].isEmpty()) {
                return "No File";
            }
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
 
            name = file[i].getOriginalFilename();
            String Fname = name.trim().substring(name.lastIndexOf("\\") + 1);
 
            String fileType = name.substring(name.lastIndexOf("."));
            size = file[i].getSize();
            totalSize += size;
 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String timePath = sdf.format(new Date().getTime());
            timePath = timePath.replace("-", "");
            File desFile = null;
            String path2 = null;
            String webpath = null; // 映射地址
            boolean rename = false; // 检查是否存在文件名相同的文件
            //alter: Xxx 处理资源向导数据文件资源文件上传问题 20181226
            Res_ExtFileSource resExtFileSource = new Res_ExtFileSource();
            if(resourceid != null && !resourceid.isEmpty()) {
                resExtFileSource = resExtFileSourceService.selectByPrimaryKey(Integer.parseInt(resourceid));
            }
            String path = null;
            if (sourcetype.equals("文件夹")) {
                Filename = Fname;
 
                if (resExtFileSource != null && resExtFileSource.getServerurl() != null) { // 判断数据库服务地址字段是否为空
                    path = sysConfig.getUploadPath() + resExtFileSource.getServerurl().replaceAll("/", "\\\\");
                    File filePath = new File(path);
                    File[] files = filePath.listFiles();
                    if (filePath.isDirectory()) {
                        String[] fileLists = filePath.list();
                        for (int j = 0; j < fileLists.length; j++) {
                            File file1 = new File(fileLists[j]);
                            if (Filename.equals(file1.getName())) {
                                rename = true;
                                break;
                            }
                        }
                    }
                    if (!rename) {
                        if (!filePath.exists()) { // 当前数据库服务地址不为空,判断该路径是否存在,不存在则创建新的文件夹
                            path = sysConfig.getUploadPath() + "FileSource\\wenjianjia\\" + timePath + "\\" + resourceid + "\\";
                            File newfilePath = new File(path); // 创建对应的年月文件夹
                            newfilePath.mkdirs();
                            webpath = "FileSource/wenjianjia/" + timePath + "/" + resourceid;
                        } else {
                            webpath = "FileSource/wenjianjia/" + timePath + "/" + resourceid; // 类型为文件夹则指向到文件夹
                        }
                    }
                } else { // 当前数据库服务地址为空,则直接创建新的文件夹
                    path = sysConfig.getUploadPath() + "FileSource\\wenjianjia\\" + timePath + "\\" + resourceid + "\\";
                    File newfilePath = new File(path); // 创建对应的年月文件夹
                    if (!newfilePath.exists()) {
                        newfilePath.mkdirs();
                    }
                    webpath = "FileSource/wenjianjia/" + timePath + "/" + resourceid; // 类型为文件夹则指向到文件夹
                }
                // path2 = env.getProperty("webroot") + "wenjianjia/";
                if (!rename) {
                    path = sysConfig.getUploadPath() + "FileSource\\wenjianjia\\" + timePath + "\\" + resourceid + "\\";
                    desFile = new File(path + Filename);
                    String info = desFile.getAbsolutePath();
                    System.out.println("info:" + info);
                }
            }
            if (sourcetype.equals("文件")) {
                Filename = uuid + fileType;
                // 当文件资源类型为文件时,判断是否该资源已经存在文件,如果有则先删除再执行上传新文件
                if (resExtFileSource != null && resExtFileSource.getServerurl() != null) {
                    String dirpath = resExtFileSource.getServerurl().substring(0, resExtFileSource.getServerurl().lastIndexOf("/") + 1);
                    String oldfile = sysConfig.getUploadPath() + resExtFileSource.getServerurl().replaceAll("/", "\\\\");
                    String ofile = sysConfig.getUploadPath() + dirpath.replaceAll("/", "\\\\");
//                    DeleteFileUtils.deletefileUtils(oldfile);
                    File file1 = new File(ofile);
                    if (!file1.exists()) {
                        path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\" + timePath + "\\";
                        File newfilePath = new File(path); // 创建对应的年月文件夹
                        newfilePath.mkdirs();
                        webpath = "temp/FileSource/wenjian/" + timePath + "/" + Filename;  // 类型为文件指向文件
                    } else {
                        path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\" + timePath + "\\";
                        //文件网络地址
                        webpath = "temp/FileSource/wenjian/" + timePath + "/" + Filename; // 类型为文件指向文件
                    }
                } else {
                    path = sysConfig.getUploadPath() + "temp\\FileSource\\wenjian\\" + timePath + "\\";
                    File filePath = new File(path); // 创建对应的年月文件夹
                    if (!filePath.exists()) {
                        filePath.mkdirs();
                    }
                    //文件网络地址
                    webpath = "temp/FileSource/wenjian/" + timePath + "/" + Filename; // 类型为文件指向文件
                }
                desFile = new File(path + Filename);
                String info = desFile.getAbsolutePath();
                System.out.println("info:" + info);
            }
            if (!rename) {
                //当sourcetype.equals("文件")
                String finasize = "";
                if (sourcetype.equals("文件")) {
                    finasize = com.landtool.lanbase.common.utils.FileUtils.setSize(size);
                } else {
                    final long total = com.landtool.lanbase.common.utils.FileUtils.getTotalSizeOfFilesInDir(new File(path));
                    finasize = com.landtool.lanbase.common.utils.FileUtils.setSize(total + size);
                }
                try {
 
                    FileUtils.copyInputStreamToFile(file[i].getInputStream(), desFile);
                    Filenames.add(Fname);
                    finasizes.add(com.landtool.lanbase.common.utils.FileUtils.setSize(size));
                    res = "{'result':'1','finasizes':'" + finasizes + "','name':'" + Filenames + "','Path':'" + webpath + "','Filename':'" + name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf(".")) + "','size':'" + finasize.substring(0, finasize.lastIndexOf(" ")) + "','unit':'" + finasize.substring(finasize.lastIndexOf(" ") + 1, finasize.length()) + "'}";
                    System.out.println(res);
                } catch (IOException e) {
                    e.printStackTrace();
                    res = "{'result':'0'}";
                }
            } else {
                res = "{'result':'3'}";
            }
 
        }
        return res;
 
    }
 
    /**
     * @param filepath 文件路径
     * @return
     * @Description: 文件删除
     * @author ykm
     * @date 2018/4/19 16:38
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/deletefile")
    public String deletefile(String filepath, String sourcetype, String name, Res_ExtFileSource resExtFileSource) {
        String res = "";
        String lujin = filepath;
        if (sourcetype.equals("文件夹")) {
 
            filepath = sysConfig.getUploadPath() + filepath.replaceAll("/", "\\\\") + "\\" + name;
            //读取删除之后文件夹大小
            boolean flag = DeleteFileUtils.deletefileUtils(filepath);
            final long total = com.landtool.lanbase.common.utils.FileUtils.getTotalSizeOfFilesInDir(new File(sysConfig.getUploadPath() + lujin.replaceAll("/", "\\\\")));
            String finasize = com.landtool.lanbase.common.utils.FileUtils.setSize(total);
            res = "{'result':'" + flag + "','size':'" + finasize.substring(0, finasize.lastIndexOf(" ")) + "','unit':'" + finasize.substring(finasize.lastIndexOf(" ") + 1, finasize.length()) + "'}";
            //将大小写入 数据库
            resExtFileSource.setFilesize((double) (total / 1000));
            resExtFileSourceService.updateByPrimaryKeySelective(resExtFileSource);
        }
        if (sourcetype.equals("文件")) {
            filepath = sysConfig.getUploadPath() + filepath.replaceAll("/", "\\\\") + "\\" + name;
            boolean flag = DeleteFileUtils.deletefileUtils(filepath);
            res = "{'result':'" + flag + "','size':'null','unit':'null'}";
        }
        return res;
    }
 
    /**
     * @Author: lizhao
     * @Date: 2018-04-25 16:23
     * @param查询本地固定地址的文件
     * @Description:
     */
 
    @ResponseBody
    @RequestMapping("/resExtFileSource/OpenFileName")
    public String OpenFileName(Model model) {
 
        File file = new File(wenjianPath());
        // 获取该目录下的所有文件大小
        File[] files = file.listFiles();
 
        // 获取文件名称
        String[] fileLists = file.list();
        StringBuilder rsb = new StringBuilder();
 
        rsb.append("{'topics':[");
        for (Integer i = 0; i < files.length; i++) {
            // 大小
            File file2 = new File(files[i].getAbsolutePath());
            String size = FileUtils.byteCountToDisplaySize(file2.length());
            // 名称
            File file1 = new File(fileLists[i]);
            String name = file1.getName();
            if (i != 0) {
                rsb.append(",");
            }
            rsb.append("{'name':'" + name + "'");
            rsb.append(",'size':'" + size + "'}");
        }
        rsb.append("]}");
        return rsb.toString();
    }
 
    @ResponseBody
    @RequestMapping("/resExtFileSource/FuWuDiZi")
    public String FuWuDiZi(String url) {
        String NeiRong = "";
        try {
            NeiRong = HttpOperateUtils.httpGet(url);
        } catch (IOException e) {
            e.printStackTrace();
            NeiRong = "失败";
        }
        return NeiRong;
    }
 
    @RequestMapping("/resExtFileSource/download")
    public void download(HttpServletResponse response, String path, String Title, HttpServletRequest request) {
 
        System.out.println(sysConfig.getUploadPath() + path.replaceAll("/", "\\\\").replace("\\uploadPath", ""));
        System.out.println(Title);
        com.landtool.lanbase.common.utils.FileUtils.download(sysConfig.getUploadPath() + path.replaceAll("/", "\\\\").replace("\\uploadPath", ""), Title, response);
 
    }
 
    /**
     * /*jsoup只支持http、https协议连接,不支持ftp
     */
    @ResponseBody
    @RequestMapping("/resExtFileSource/getUrlList")
    public String jsoupDetail(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            StringBuilder sb = new StringBuilder();
 
            //目前只考虑了目录结构为table和pre标签下的目录,即Tomcat和IIS
            Elements tableElements = doc.getElementsByTag("table"); //http://www.zhjg.com:8080/javapubzy/BootStrap4/
            Elements preElements = doc.getElementsByTag("pre");     //http://10.1.4.27/shuqu/
            if (tableElements.size() > 0) {
                Document tableDoc = Jsoup.parse(tableElements.toString());
                int trSize = tableDoc.select("tr").size();
                int tdSize = tableDoc.select("tr").first().select("td").size(); //获取第一行tr下的td数
                String[][] tableList = new String[trSize][2];
                int tr = -1;
                int td = -1;
                for (int i = 0; i < tdSize; i++) {
                    tableList[0][i] = tableDoc.select("tr").get(0).select("td").get(i).text();
                    if (tableList[0][i].equals("Filename") && tr == -1) {
                        tr = i;
                    }
                    if (tableList[0][i].equals("Size") && td == -1) {
                        td = i;
                    }
                    if (tr != -1 && td != -1) {
                        break;
                    }
                }
                sb.append("[");
                for (int i = 1; i < trSize; i++) {
                    if (i != 1) sb.append(",");
                    tableList[i][0] = tableDoc.select("tr").get(i).select("td").get(tr).select("A").get(0).text();
                    tableList[i][1] = tableDoc.select("tr").get(i).select("td").get(td).select("tt").get(0).text();
                    sb.append("{");
                    sb.append("filename: '" + tableList[i][0] + "'");
                    sb.append(",size: '" + tableList[i][1] + "'");
                    sb.append("}");
                }
                sb.append("]");
                return sb.toString();
            } else if (preElements.size() > 0) {
                int aSize = preElements.select("A").size(); //获取a标签个数
                sb.append("[");
                for (int i = 1; i < aSize; i++) {
                    if (i != 1) sb.append(",");
                    String aName = preElements.select("A").get(i).text();
                    sb.append("{");
                    sb.append("filename: '" + aName + "'");
                    sb.append(",size: ''");
                    sb.append("}");
                }
                sb.append("]");
                return sb.toString();
            } else {
                sb.append("[{");
                sb.append("filename: '无法获取该地址下的目录!'");
                sb.append(",size: ''");
                sb.append("}]");
                return sb.toString();
            }
 
        } catch (IOException e) {
            e.printStackTrace();
            return "[{filename:'无法获取该地址下的目录!',size:''}]";
        }
    }
 
    @RequestMapping("/ResManage/ResRegister/NcFileConfig")
    public String NcFileConfig(int resourceType,int resourceid,Model model){
        Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid);
        if(res_ExtFileSource.getNcconfig()!=null && !res_ExtFileSource.getNcconfig().equals("")){
            JSONObject jsonObject = JSONObject.parseObject(res_ExtFileSource.getNcconfig());
            JSONArray jsonArray = jsonObject.getJSONArray("ncconfig");
            NcConfig ncConfig = JSONUtil.toBean(String.valueOf(jsonArray.get(0)), NcConfig.class);
            model.addAttribute("ncConfig", ncConfig);
        }
        model.addAttribute("pubzyWebRoot", sysConfig.getPubzyWebRoot());
        model.addAttribute("ncFileConfigURL", sysConfig.getNcFileConfigURL()+"?resourceType="+resourceType+"&uploadPath="+sysConfig.getUploadPath()+res_ExtFileSource.getServerurl());
        model.addAttribute("readNcFileURL", sysConfig.getReadNcFileURL());
        model.addAttribute("res_ExtFileSource", res_ExtFileSource);
        model.addAttribute("resourceid", resourceid);
        model.addAttribute("resourceType", resourceType);
        model.addAttribute("uploadPath", res_ExtFileSource.getServerurl());
        model.addAttribute("absolutePath", sysConfig.getUploadPath()+res_ExtFileSource.getServerurl());
        return "ResManage/ResRegister/NcFileConfig";
    }
    
    @RequestMapping("/ResManage/ResRegister/StorageConfig")
    public String StorageConfig(int resourceType,int resourceid,Model model){
        Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid);
        if(res_ExtFileSource.getNcconfig()!=null && !res_ExtFileSource.getNcconfig().equals("")){
            JSONObject jsonObject = JSONObject.parseObject(res_ExtFileSource.getNcconfig());
            JSONArray jsonArray = jsonObject.getJSONArray("ncconfig");
            NcConfig ncConfig = JSONUtil.toBean(String.valueOf(jsonArray.get(0)), NcConfig.class);
            model.addAttribute("ncConfig", ncConfig);
        }
        Res_StorageConfig resStorageConfig = resStorageConfigService.findStorageConfigByResourceid(resourceid);
        if(resStorageConfig!=null && resStorageConfig.getVariableparams()!=null && !resStorageConfig.getVariableparams().equals("")){
            JSONArray jsonarray = JSONArray.parseArray(resStorageConfig.getVariableparams());
            model.addAttribute("jsonarray", jsonarray);
        }
        if(resStorageConfig!=null && resStorageConfig.getUvparams()!=null && !resStorageConfig.getUvparams().equals("")){
            JSONArray uvjson = JSONArray.parseArray(resStorageConfig.getUvparams());
            model.addAttribute("uvjson", uvjson);
        }
        model.addAttribute("resStorageConfig", resStorageConfig);
        model.addAttribute("pubzyWebRoot", sysConfig.getPubzyWebRoot());
        model.addAttribute("ncFileConfigURL", sysConfig.getNcFileConfigURL()+"?resourceType="+resourceType+"&uploadPath="+sysConfig.getUploadPath()+res_ExtFileSource.getServerurl());
        model.addAttribute("readNcFileURL", sysConfig.getReadNcFileURL());
        model.addAttribute("res_ExtFileSource", res_ExtFileSource);
        model.addAttribute("resourceid", resourceid);
        model.addAttribute("resourceType", resourceType);
        model.addAttribute("ncSyncStorageUrl", sysConfig.getNcStorageUrl().substring(0, sysConfig.getNcStorageUrl().length()-9)+"jobs/");
        return "ResManage/ResRegister/StorageConfig";
    }
    
    @RequestMapping("/ResManage/ResRegister/updateNcJsonPath")
    @ResponseBody
    public int updateNcJsonPath(Res_ExtFileSource res_ExtFileSource){
        return resExtFileSourceService.updateByPrimaryKeySelective(res_ExtFileSource);
    }
    
    //保存入库配置
    @RequestMapping("/ResManage/ResRegister/saveStorageConfig")
    @ResponseBody
    public int saveStorageConfig(Res_StorageConfig res_storageConfig){
        Res_StorageConfig storage = resStorageConfigService.findStorageConfigByResourceid(res_storageConfig.getResourceid());
        if(storage==null){
            return resStorageConfigService.insertStorageConfig(res_storageConfig);
        }else{
            return resStorageConfigService.updateStorageConfig(res_storageConfig);
        }
    }
    
    //NC配置
    @RequestMapping("/ResManage/ResRegister/getNcConfig")
    @ResponseBody
    public String getNcConfig(int resourceid){
        Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(resourceid);
        return res_ExtFileSource.getNcconfig();
    }
        
    //入库配置
    @RequestMapping("/ResManage/ResRegister/findParams")
    @ResponseBody
    public String findParams(int resourceid){
        String params = "";
        Res_StorageConfig res_storageConfig = resStorageConfigService.findStorageConfigByResourceid(resourceid);
        if(res_storageConfig!=null && res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){
            params = res_storageConfig.getVariableparams();
        }else if(res_storageConfig!=null && res_storageConfig.getUvparams()!=null && !res_storageConfig.getUvparams().equals("")){
            params = res_storageConfig.getUvparams();
        }
        return params;
    }
    
    //清空配置
    @RequestMapping("/ResManage/ResRegister/clearConfig")
    @ResponseBody
    public int clearConfig(int resourceid,int type){
        int total=0;
        switch (type) {
        case 1:    //nc配置清空
            Res_ExtFileSource resExtFileSource = new Res_ExtFileSource();
            resExtFileSource.setResourceid(resourceid);
            resExtFileSource.setNcconfig("");
            total = resExtFileSourceService.updateByPrimaryKeySelective(resExtFileSource);
            break;
 
        case 2: //入库配置清空
            total = resStorageConfigService.deleteByResourceid(resourceid);
            break;
            
        default: 
            Res_ExtFileSource res_ExtFileSource = new Res_ExtFileSource();
            res_ExtFileSource.setResourceid(resourceid);
            res_ExtFileSource.setNcconfig("");
            res_ExtFileSource.setNcjsonattribute("");
            res_ExtFileSource.setNcjsonpath("");
            total = resExtFileSourceService.updateByPrimaryKeySelective(res_ExtFileSource);
            total+= resStorageConfigService.deleteByResourceid(resourceid);
            break;
        }
        return total;
    }
    
    //调用服务入库
    @RequestMapping("/ResManage/ResRegister/saveStorage")
    @ResponseBody
    public String saveStorage(int resourceid,String fileName,String param){
        Res_StorageConfig res_storageConfig = resStorageConfigService.findStorageConfigByResourceid(resourceid);
        if(res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){
            res_storageConfig.setVariableparams(param);
        }else{
            res_storageConfig.setUvparams(param);
        }
        String databaseid ="";
        if(res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){
            databaseid = JSONObject.parseObject(res_storageConfig.getVariableparams()).get("databaseid").toString();            
        }else if(res_storageConfig.getUvparams()!=null && !res_storageConfig.getUvparams().equals("")){
            databaseid = JSONObject.parseObject(res_storageConfig.getUvparams()).get("databaseid").toString();        
        }
        Res_ExtDataSource res_ExtDataSource = resExtDataSourceService.selectByPrimaryKey(Integer.valueOf(databaseid));
        Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(res_storageConfig.getResourceid());
        String ncFilePath = sysConfig.getUploadRootPath()+res_ExtFileSource.getServerurl();
        String body ="";
        List<String> jobids = new ArrayList<String>();
        NcMessage message = new NcMessage();
        body = resStorageConfigService.invoke(res_storageConfig,res_ExtDataSource,ncFilePath+"/"+fileName);    
        String jobId = (String) JSONObject.parseObject(body).get("jobId");
        String messages = (String) JSONObject.parseObject(body).get("messages");
        if(jobId!=null && !jobId.equals("")){
            message.setCode(1);
            jobids.add(jobId);
        }else{
            message.setCode(2);     //返回包含result message 代表同步
            if(messages!=null && !messages.equals("")){
                JSONArray jsonArray = JSON.parseArray(messages);
                String description = jsonArray.getJSONObject(jsonArray.size()-1).get("description").toString();
                if(description.contains("Succeeded")){
                    message.setMessage("success");
                }
            }
            if(body.contains("error")){
                message.setMessage("error");
            }
        }
        message.setJobids(jobids);
        return JSON.toJSON(message).toString();
    }
 
    //异步调用服务入库
    @RequestMapping("/ResManage/ResRegister/syncSaveStorage")
    @ResponseBody
    public String syncSaveStorage(Res_StorageConfig res_storageConfig,int resourceType){
        String databaseid ="";
        if(res_storageConfig.getVariableparams()!=null && !res_storageConfig.getVariableparams().equals("")){
            databaseid = JSONObject.parseObject(res_storageConfig.getVariableparams()).get("databaseid").toString();            
        }else if(res_storageConfig.getUvparams()!=null && !res_storageConfig.getUvparams().equals("")){
            databaseid = JSONObject.parseObject(res_storageConfig.getUvparams()).get("databaseid").toString();        
        }
        Res_ExtDataSource res_ExtDataSource = resExtDataSourceService.selectByPrimaryKey(Integer.valueOf(databaseid));
        Res_ExtFileSource res_ExtFileSource = resExtFileSourceService.selectByPrimaryKey(res_storageConfig.getResourceid());
        String ncFilePath = sysConfig.getUploadRootPath()+res_ExtFileSource.getServerurl();
        String body ="";
        List<String> jobids = new ArrayList<String>();
        NcMessage message = new NcMessage();
        if(resourceType==1){    //文件
            body = resStorageConfigService.invoke(res_storageConfig,res_ExtDataSource,ncFilePath);    
            System.out.println(body);
            String jobId = (String) JSONObject.parseObject(body).get("jobId");
            String messages = (String) JSONObject.parseObject(body).get("messages");
            if(jobId!=null && !jobId.equals("")){    //返回包含jobId 代表异步
                message.setCode(1);
                jobids.add(jobId);
            }else{
                message.setCode(2);     //返回包含result message 代表同步
                if(messages!=null && !messages.equals("")){
                    JSONArray jsonArray = JSON.parseArray(messages);
                    String description = jsonArray.getJSONObject(jsonArray.size()-1).get("description").toString();
                    if(description.contains("Succeeded")){
                        message.setMessage("success");
                    }
                }
                if(body.contains("error")){
                    message.setMessage("error");
                }
            }
        }else{    //资源目录
            File files = new File(sysConfig.getUploadPath().replace("\\", "/")+res_ExtFileSource.getServerurl());
            File[] listFiles = files.listFiles();
            if(listFiles.length>0){
                for(File file : listFiles){
                    if(!file.isDirectory() && file.getName().contains(".nc")){
                        String path =ncFilePath+"/"+file.getName();
                        body = resStorageConfigService.invoke(res_storageConfig,res_ExtDataSource,path);    
                        System.out.println(body);
                        String jobId = (String) JSONObject.parseObject(body).get("jobId");
                        String messages = (String) JSONObject.parseObject(body).get("messages");
                        if(jobId!=null && !jobId.equals("")){
                            message.setCode(1);
                            jobids.add(jobId);
                        }else{
                            message.setCode(2);
                            if(messages!=null && !messages.equals("")){
                                JSONArray jsonArray = JSON.parseArray(messages);
                                String description = jsonArray.getJSONObject(jsonArray.size()-1).get("description").toString();
                                if(description.contains("Succeeded")){
                                    message.setMessage("success");
                                }
                            }
                            if(body.contains("error")){
                                message.setMessage("error");
                            }
                        }
                    }
                }
            }
        }
        message.setJobids(jobids);
        return JSON.toJSON(message).toString();
    }
    
    @RequestMapping("/ResManage/ResRegister/saveStorageRelation")
    @ResponseBody
    public void saveStorageRelation(Res_Storage_Relation storageRelation){
        Res_Storage_Relation res_Storage_Relation = resStorageRelaTionService.selectByResourceidAndFileName(storageRelation);
        if(res_Storage_Relation==null){
            storageRelation.setIsstorage(1);
            resStorageRelaTionService.insert(storageRelation);
        }else{
            storageRelation.setIsstorage(1);
            resStorageRelaTionService.update(storageRelation);
        }
    }
    
    @RequestMapping("/ResManage/ResRegister/insertStorageRelation")
    @ResponseBody
    public void insertStorageRelation(Res_Storage_Relation storageRelation){
        Res_Storage_Relation res_Storage_Relation = resStorageRelaTionService.selectByResourceidAndFileName(storageRelation);
        if(res_Storage_Relation==null){
            storageRelation.setIsgeneration(1);
            resStorageRelaTionService.insert(storageRelation);
        }else{
            storageRelation.setIsgeneration(1);
            resStorageRelaTionService.update(storageRelation);
        }
    }
}