lyh
2025-01-13 137d008bd9b7d932160436a3a560b24512f6d1db
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
package org.jeecg.modules.dnc.utils.file;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.FileUploadResult;
import org.jeecg.common.util.SHA256Util;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.utils.date.DateUtil;
import org.jeecg.modules.dnc.response.DocumentCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
@Slf4j
@Component
public class FileUtilS {
 
    private static String fileUploadFolder;
 
    @Value("${fileHomePath}")
    public void setFileUploadFolder(String fileUploadFolder) {
        FileUtilS.fileUploadFolder = fileUploadFolder;
    }
 
 
    private static String fileNcFolder;
 
    @Value("${fileNCPath}")
    public void setFileNCFolder(String fileNcFolder) {
        FileUtilS.fileNcFolder = fileNcFolder;
    }
 
    private static String addOrDelete;
 
    @Value("${ncSend.addOrDelete}")
    public void addOrDelete(String addOrDelete) {
        FileUtilS.addOrDelete = addOrDelete;
    }
 
 
    /**
     * 方法一:使用 FileWriter 写文件
     * @param filepath 文件目录
     * @param content  待写入内容
     * @throws IOException
     */
    public static void fileWriterSql(String filepath, String content) throws IOException {
        OutputStreamWriter outputStreamWriter = null;
        try {
            File file = new File(filepath);
            if (!file.exists()){
                file.createNewFile();
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            if (outputStream != null){
                outputStreamWriter = new OutputStreamWriter(outputStream, "utf-8");
                outputStreamWriter.write(content);
                outputStreamWriter.flush();
            }
        }
        catch (IOException e) {
            e.getMessage();
        }
        finally {
            try {
                if (outputStreamWriter != null){
                    outputStreamWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    public static boolean copyNcFile(String lastFile,String filePath) {
        String absolutePathSend =  lastFile;
        FileChannel in = null;
        FileChannel out = null;
        try{
            in = new FileInputStream(absolutePathSend).getChannel();
            File file = new File(filePath);
            if (!file.getParentFile().exists()){
                //文件夹不存在 生成
                file.getParentFile().mkdirs();
            }
            out = new FileOutputStream(filePath, true).getChannel();
            out.transferFrom(in, 0, in.size());
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            in.close();
            out.close();
 
        }
        catch (IOException e) {
            e.printStackTrace();
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return false;
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
 
    /**
     * 新建文件夹
     * @param path
     * @return
     */
 
    public static boolean saveFileFromPath(String path) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        String absolutePath =  fileNcFolder + "/" + path ;
        try {
            boolean b = new File(absolutePath).mkdirs();
            return b;
        }catch (Exception e) {
            return false;
        }
    }
 
    /**
     * 新建文件夹
     * @param path
     * @return
     */
 
    public static boolean saveDeviceFromPath(String path) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        String absolutePathSend =  fileNcFolder + "/" + path + "/send" ;
        String absolutePathRec =  fileNcFolder + "/" + path + "/rec" ;
        try {
            boolean b = new File(absolutePathSend).mkdirs();
            boolean c = new File(absolutePathRec).mkdirs();
            return b;
        }catch (Exception e) {
            return false;
        }
    }
 
 
    /**
     * 修改文件夹
     * @param path
     * @return
     */
 
    public static boolean updateFileFromPath(String path,String lastPathName) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        String absolutePathLast =  fileNcFolder + "/" + lastPathName ;
        String absolutePathNew =  fileNcFolder + "/" + path ;
        try {
            boolean b = new File(absolutePathLast).renameTo(new File(absolutePathNew));
            return b;
        }catch (Exception e) {
            return false;
        }
    }
 
    public static boolean uploadFileSend(MultipartFile file,String path) {
        if(file == null || file.isEmpty()) {
            return false;
        }
        //绝对路径
        String absolutePath =  fileNcFolder + "/" + path + "/send/";
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        // 目标文件路径+文件名
        String targetFile = absolutePath + file.getOriginalFilename() ;
        File toFile = new File(targetFile);
        if (!toFile.getParentFile().exists()) {
            toFile.mkdirs();
        }
 
        try {
            file.transferTo(toFile);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /**
     * file转MultipartFile
     *
     * @param file file
     * @return MultipartFile
     */
    public static MultipartFile fileToMultipartFile(File file) {
        MultipartFile result = null;
        if (null != file) {
            try (FileInputStream input = new FileInputStream(file)) {
                result = new MockMultipartFile(file.getName().concat("temp"), file.getName(), "text/plain", input);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
 
    public static boolean copyFileRec(String lastFile) {
        String absolutePathSend =  lastFile;
        //字符串替换
        String absolutePathSendNotFile =  lastFile.replace("\\rec\\","\\bak_rec\\");
        String fileNameNoPath = (new File(lastFile)).getName();
        String suffix = getFileSuffix(fileNameNoPath);
        Integer recNum = absolutePathSendNotFile.lastIndexOf(fileNameNoPath);
        if (suffix == null) {
 
            absolutePathSendNotFile = absolutePathSendNotFile.substring(0,recNum) + fileNameNoPath+ "-" +
                    DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL);
           /* absolutePathSendNotFile = absolutePathSendNotFile.replace(fileNameNoPath,
                    fileNameNoPath + "-" +DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL));*/
        } else {
            String name = fileNameNoPath.replace("." + suffix,"");
            absolutePathSendNotFile = absolutePathSendNotFile.substring(0,recNum) + name+ "-" +
                    DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL) + "." + suffix;
        }
        try{
            FileChannel in = new FileInputStream(absolutePathSend).getChannel();
            File file = new File(absolutePathSendNotFile);
            if (!file.getParentFile().exists()){
                //文件夹不存在 生成
                file.getParentFile().mkdirs();
            }
            FileChannel out = new FileOutputStream(absolutePathSendNotFile, true).getChannel();
            out.transferFrom(in, 0, in.size());
            in.close();
            out.close();
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
 
    public static boolean fileRecDelete(String lastFile) {
        String absolutePathSend =  lastFile;
        Path path = Paths.get(lastFile);
        try {
            System.gc();
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try{
            File file = new File(lastFile);
            file.delete();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public static Boolean deleteFile(File file) {
        //判断文件不为null或文件目录存在
        if (file == null || !file.exists()) {
            System.out.println("文件删除失败,请检查文件是否存在以及文件路径是否正确");
            return false;
        }
        //获取目录下子文件
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f : files) {
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                //递归删除目录下的文件
                deleteFile(f);
            } else {
                //文件删除
                f.delete();
                //打印文件名
                System.out.println("文件名:" + f.getName());
            }
        }
        //文件夹删除
        file.delete();
        System.out.println("目录名:" + file.getName());
        return true;
    }
 
    public static boolean copyFile(String lastFile ,String newFile, String fileName) {
        String absolutePathSend =  fileNcFolder + "/" + newFile + "/send/" + fileName;
        String absolutePathSendNotFile =  fileNcFolder + "/" + newFile + "/send/";
        String absolutePath =  fileUploadFolder + "/" + lastFile;
        try{
            File toFile = new File(absolutePathSendNotFile);
            if("true".equals(addOrDelete)) {
                deleteFile(toFile);
            }
            toFile.mkdirs();
            FileChannel in = new FileInputStream(absolutePath).getChannel();
            //查询文件是否存在
            File file = new File(absolutePathSend);
            //判断文件或文件夹是否存在
            boolean flag = file.exists();
            if(flag) {
                //文件存在就要删除文件
                System.gc();
                file.delete();
            }
            String suffix = FileUtilS.getFileSuffix(fileName);
            String newFileName = FileUtilS.getFilenameNonSuffix(fileName);
            String zip = fileNcFolder + "/" + newFile + "/send/" + newFileName + "/";
 
            FileChannel out = new FileOutputStream(absolutePathSend, true).getChannel();
            out.transferFrom(in, 0, in.size());
            if (suffix.equals("zip")) {
                try {
                    FileZipUtil.unZipFiles(absolutePathSend,zip);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
 
 
 
    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp;
        for (int i = 0; i < tempList.length; i++) {
            if (filePath.endsWith(File.separator)) {
                temp = new File(filePath + tempList[i]);
            } else {
                temp = new File(filePath + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                // 先删除文件夹里面的文件
                deleteFile(filePath + "/" + tempList[i]);
                // 再删除空文件夹
                deleteFile(filePath + "/" + tempList[i]);
                flag = true;
            }
        }
        return flag;
    }
 
 
    /**
     * 删除文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteNcFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        if (!file.exists()) {
            return flag;
        }
        boolean b = file.delete();
        if (!b) {
            System.out.println("文件删除失败: " + filePath);
        }
        return true;
    }
 
    /**
     * 集成数据
     * @param lastFile
     * @param newFile
     * @param zipString
     * @return
     */
    public static boolean copyFileNcIntegration(String lastFile ,String newFile,String zipString) {
        String lastFileReName =  fileNcFolder + "/" + lastFile ;
        if (StringUtils.isNotBlank(zipString)) {
            lastFileReName = lastFileReName + "." + zipString;
            newFile = newFile + "." + zipString;
        }
        FileChannel in = null;
        FileChannel out = null;
        try{
            in = new FileInputStream(lastFileReName).getChannel();
            out = new FileOutputStream(newFile, true).getChannel();
            out.transferFrom(in, 0, in.size());
            in.close();
            out.close();
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
    }
 
 
 
 
    /**
     * 测试 更换名字
     * @param lastFile
     * @param newFile
     * @param
     * @return
     */
    public static boolean copyFileUpName(String lastFile ,String newFile,String fixFileName,String ncFix) {
        String lastFileReName =  fileNcFolder + "/" + lastFile ;
        if (StringUtils.isNotBlank(fixFileName)) {
            lastFileReName = lastFileReName + "." + fixFileName;
            newFile = newFile + "." + ncFix;
        }
        try {
            long begintime = System.currentTimeMillis(); // 获取拷贝文件前的系统时间
 
            Files.copy(Paths.get(lastFileReName),
                    Paths.get(newFile),
                    StandardCopyOption.REPLACE_EXISTING);
            //方法二
            /*// 创建一个带缓冲区的输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(lastFileReName));
            // 创建一个带缓冲区的输出流
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
 
            // 定义一个字节数组,作为缓冲区
            byte[] buff = new byte[1024];
            long begintime = System.currentTimeMillis(); // 获取拷贝文件前的系统时间
            int len;
            while ((len = bis.read(buff)) != -1) {
                bos.write(buff, 0, len); // 从第一个字节开始,向文件写入len个字节
            }
 
 
            bis.close();
            bos.close(); */
            long endtime = System.currentTimeMillis(); // 获取文件拷贝结束时的系统时间
            System.out.println("拷贝文件所消耗的时间是:" + (endtime - begintime) + "毫秒");
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    public static String fileSizeNC(String lastFile,String fileName){
        String absolutePathSend =  fileUploadFolder + "/" + lastFile + "/" + fileName;
        File file = new File(absolutePathSend);
        if (!file.exists()) {
            return "0";
        }
        return String.valueOf(file.length());
    }
 
    /*产品树到设备树 NC*/
    public static boolean copyFileNc(String lastFile ,String newFile, String fileName,String newFileName,String zipString) {
        String absolutePathSend =  fileUploadFolder + "/" + lastFile + "/" + fileName;
        //判断文件夹是否存在
        File file = new File(fileNcFolder + "/" + newFile + "/send/");
        if (!file.exists()) {
            file.mkdirs();
        }
        String abNewFileReName =  fileNcFolder + "/" + newFile + "/send/" + newFileName ;
        if (StringUtils.isNotBlank(zipString)) {
            abNewFileReName = abNewFileReName + "." + zipString;
        }
        String zip = fileNcFolder + "/" + newFile + "/send/" + newFileName + "/";
        try{
            FileChannel in = new FileInputStream(absolutePathSend).getChannel();
            FileChannel out = new FileOutputStream(abNewFileReName, true).getChannel();
            out.transferFrom(in, 0, in.size());
            if (zipString.equals("zip")) {
                try {
                    FileZipUtil.unZipFiles(abNewFileReName,zip);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            in.close();
            out.close();
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
 
 
    public static void compressFolder(File folder, String zipFilePath) throws IOException {
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
        compressFolder(folder, folder.getName(), zipOut);
        zipOut.close();
    }
 
    private static void compressFolder(File folder, String parentFolder, ZipOutputStream zipOut) throws IOException {
        if (folder.isFile()) {
            String filePath = folder.getPath();
            String entryPath = filePath.substring(parentFolder.length() + 1);
            ZipEntry zipEntry = new ZipEntry(entryPath);
            zipOut.putNextEntry(zipEntry);
            FileInputStream fileIn = new FileInputStream(filePath);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileIn.read(buffer)) != -1) {
                zipOut.write(buffer, 0, bytesRead);
            }
            fileIn.close();
            zipOut.closeEntry();
        } else {
            for (File file : folder.listFiles()) {
                compressFolder(file, parentFolder, zipOut);
            }
        }
    }
    /*产品树到设备树 NC*/
    public static boolean copyFileNcToBak(String newFile, String newFileName,String fixFileName) {
        String stringDate = DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_STRING);
        String absolutePathSend = null,abNewFileReName= null;
        absolutePathSend =  fileNcFolder + "/" + newFile + "/send/" + newFileName ;
        abNewFileReName =  fileNcFolder + "/" + newFile + "/send_bak/" + "/" +stringDate+ "/" + newFileName + "_" + DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL)  ;
        if (StringUtils.isNotBlank(fixFileName) ) {
            if(!fixFileName.equals("zip")) {
                absolutePathSend =  absolutePathSend + "." + fixFileName ;
                abNewFileReName = abNewFileReName + "." + fixFileName ;
            } else {
                copyFileBakZip(new File(absolutePathSend),new File(abNewFileReName));
                return true;
            }
        }
        File targetFile = new File(abNewFileReName);
        if(!targetFile.getParentFile().exists()){
            targetFile.getParentFile().mkdirs();
        }
        try{
            FileChannel in = new FileInputStream(absolutePathSend).getChannel();
            FileChannel out = new FileOutputStream(abNewFileReName, true).getChannel();
            out.transferFrom(in, 0, in.size());
            in.close();
            out.close();
            return true;
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
 
    }
 
    public static Boolean deletePathNameFile(File file) {
        //判断文件不为null或文件目录存在
        if (file == null || !file.exists()) {
            System.out.println("文件删除失败,请检查文件是否存在以及文件路径是否正确");
            return false;
        }
        //获取目录下子文件
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f : files) {
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                //递归删除目录下的文件
                deletePathNameFile(f);
            } else {
                //文件删除
                f.delete();
                //打印文件名
                System.out.println("文件名:" + f.getName());
            }
        }
        //文件夹删除
        file.delete();
        return true;
    }
 
 
    public static Boolean deleteZipFromToSend(String newFile,String newFileName,String zipString) {
        String abNewFileReName =  fileNcFolder + "/" + newFile + "/send/" + newFileName ;
        if (StringUtils.isNotBlank(zipString)) {
            if (zipString.equals("zip")) {
                abNewFileReName = abNewFileReName + "." + zipString;
                return fileRecDelete(abNewFileReName);
            }
        }
        return true;
    }
 
    public static Boolean deleteFilePathZip(String newFile,String newFileName,String zipString) {
        String abNewFileReName =  fileNcFolder + "/" + newFile + "/send/" + newFileName ;
        //分类
        if (StringUtils.isNotBlank(zipString)) {
            //有问题
            if (zipString.equals("zip")) {
                String stringDate =  DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_STRING);
                String abNewFileReNameBak =  fileNcFolder + "/" + newFile + "/send_bak/" + "/" +stringDate+ "/" + newFileName + "_" +
                        DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_FULL)  ;
                copyFileBakZip(new File(abNewFileReName),new File(abNewFileReNameBak));
                return deleteFile(new File(abNewFileReName));
            }
            abNewFileReName = abNewFileReName + "." + zipString;
        }
        copyFileNcToBak(newFile,newFileName,zipString);
        return fileRecDelete(abNewFileReName);
    }
 
    public static void copyFileBakZip(File file, File target) {
        target = new File(target, file.getName());
        if (!target.exists()) {
            target.mkdirs();
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory()) {
                    copyFileBakZip(f, target);
                }
                else {
                    copyFileDeleteZip(f, target);
                }
            }
        } else {
            copyFileDeleteZip(file, target);
        }
    }
 
    public static void copyFileDeleteZip(File file, File directory) {
        try {
            FileInputStream input = new FileInputStream(file);
            FileOutputStream output = new FileOutputStream(new File(directory, file.getName()));
            byte[] buff = new byte[8192];
            int len = -1;
            while ((len = input.read(buff)) > -1) {
                output.write(buff);
            }
            input.close();
            output.close();
        } catch (Exception e) {
 
        }
    }
 
 
    //改名
    public static FileUploadResult uploadFileUpdateFileName(MultipartFile file,String fileName ,String suffix) {
        if(file == null || file.isEmpty())
            return null;
        Date currentDate = DateUtil.getNow();
        String monthStr = DateUtil.format(currentDate, DateUtil.STR_YEAR_MONTH);
        //相对路径
        String relativePath = "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/";
        //绝对路径
        String absolutePath =  fileUploadFolder + "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/";
        String fileNameNonSuffix = getFilenameNonSuffix(fileName);
        if(fileNameNonSuffix == null)
            return null;
        String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis());
        Long fileSize = file.getSize();
        boolean b = uploadFile(file, absolutePath, encodeFileName);
        if(!b)
            return null;
        FileUploadResult dto = new FileUploadResult();
        dto.setFileName(fileNameNonSuffix);
        dto.setFileEncodeName(encodeFileName);
        dto.setFilePath(relativePath);
        dto.setFileSize(fileSize);
        dto.setFileSuffix(suffix);
        return dto;
    }
 
    public static boolean deleteFileNewRec(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        if (!file.exists()) {
            return flag;
        }
        boolean b = file.delete();
        if (!b) {
            try {
                System.gc();
                Path path = Paths.get(filePath);
                Files.deleteIfExists(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
 
 
    public static FileUploadResult uploadFile(MultipartFile file) {
        if(file == null || file.isEmpty())
            return null;
        String fileName = file.getOriginalFilename();
        String suffix = getFileSuffix(fileName);
//        if(StringUtils.isBlank(suffix))
//            return null;
        Date currentDate = DateUtil.getNow();
        String monthStr = DateUtil.format(currentDate, DateUtil.STR_YEAR_MONTH);
        //相对路径
        String relativePath = "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/";
        //绝对路径
        String absolutePath =  fileUploadFolder + "/" + monthStr + "/" + DateUtil.getDayStr(currentDate) + "/";
        String fileNameNonSuffix = getFilenameNonSuffix(fileName);
        if(fileNameNonSuffix == null)
            return null;
        String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis());
        Long fileSize = file.getSize();
        boolean b = uploadFile(file, absolutePath, encodeFileName);
        if(!b)
            return null;
        FileUploadResult dto = new FileUploadResult();
        dto.setFileName(fileNameNonSuffix);
        dto.setFileEncodeName(encodeFileName);
        dto.setFilePath(relativePath);
        dto.setFileSize(fileSize);
        dto.setFileSuffix(suffix);
        return dto;
    }
 
    public static void downLoadFile(HttpServletResponse response, String fileName, String filePath, String toFileName) {
        String absolutePath = fileUploadFolder + filePath;
        File file = new File(absolutePath , fileName);
        if(file.exists()) {
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                response.setHeader("Content-Type", "application/octet-stream;charset=utf-8"); // 告诉浏览器输出内容为流
                //response.setHeader("Content-Disposition", "attachment;fileName="+ new String(toFileName.getBytes("UTF-8"),"ISO-8859-1"));
                response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(toFileName, "UTF-8"));
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            }catch (Exception e) {
                log.error(e.getMessage(), e.getStackTrace());
            }finally {
                if(bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                    }
                }
                if(fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                    }
                }
            }
        }else {
            ExceptionCast.cast(DocumentCode.DOC_PUBLISH_FILE_NOT_EXIST);
        }
    }
    /**
     * 上传文件工具类
     * @param multipartFile
     * @param path
     * @param fileNewName  新的文件名
     * @return
     */
    public static boolean uploadFile(MultipartFile multipartFile, String path, String fileNewName) {
        File targetFile = new File(path, fileNewName);
        if(!targetFile.getParentFile().exists()){
            targetFile.getParentFile().mkdirs();
        }
        try {
            multipartFile.transferTo(targetFile);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e.getStackTrace());
            return false;
        }
    }
 
    /**
     /**
     * 获取文件后缀  无后缀文件返回NULL
     * @param fileName
     * @return
     */
    public static String getFileSuffix(String fileName) {
        if (fileName.contains(".")) {
            String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
            return suffix;
        }else {
            return null;
        }
 
    }
 
    /**
     * 获取文件名 不带后缀和点号
     * @param fileName
     * @return
     */
    public static String getFilenameNonSuffix(String fileName) {
        if (fileName.contains(".")) {
            String filename = fileName.substring(0, fileName.lastIndexOf('.'));
            return filename;
        }else {
            return fileName;
        }
 
    }
 
    public static List<String> readFile(String fileEncodeName, String filePath) {
        String absolutePath = fileUploadFolder + filePath;
        File file = new File(absolutePath , fileEncodeName);
        if(!file.exists() || file.isDirectory())
            ExceptionCast.cast(DocumentCode.DOC_PUBLISH_FILE_NOT_EXIST);
        String charset = checkFileEncode(file);
        if(charset == null)
            ExceptionCast.cast(DocumentCode.DOC_PUBLISH_FILE_NOT_EXIST);
        List<String> readList = new ArrayList<>();
        String tempString = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            while ((tempString = reader.readLine()) != null) {
                readList.add(tempString);
            }
            if(readList.isEmpty())
                return null;
            return readList;
        } catch (Exception e) {
            log.error(e.getMessage(), e.getStackTrace());
            return null;
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e.getStackTrace());
                }
            }
        }
    }
 
    private static String checkFileEncode(File file) {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        BufferedInputStream bis = null;
        try {
            boolean checked = false;
            bis = new BufferedInputStream(new FileInputStream(file));
            bis.mark(0);
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1)
                return charset;
            if(first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE ) {
                charset = "UTF-16LE";
                checked = true;
            }else if ( first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF ) {
                charset = "UTF-16BE";
                checked = true;
            }else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF ) {
                charset = "UTF-8";
                checked = true;
            }
            bis.reset();
            if (!checked) {
                while ((read = bis.read()) != -1 ) {
                    if ( read >= 0xF0 )
                        break;
                    if ( 0x80 <= read && read <= 0xBF ) // 单独出现BF以下的,也算是GBK
                        break;
                    if ( 0xC0 <= read && read <= 0xDF ) {
                        read = bis.read();
                        if ( 0x80 <= read && read <= 0xBF ) // 双字节 (0xC0 - 0xDF) (0x80 - 0xBF),也可能在GBK编码内  
                            continue;
                        else
                            break;
                    } else if ( 0xE0 <= read && read <= 0xEF ) {
                        // 也有可能出错,但是几率较小
                        read = bis.read();
                        if ( 0x80 <= read && read <= 0xBF ) {
                            read = bis.read();
                            if ( 0x80 <= read && read <= 0xBF ) {
                                charset = "UTF-8";
                                break;
                            } else
                                break;
                        } else {
                            break;
                        }
                    }
                }
            }
            return charset;
        }catch (Exception e) {
            return null;
        }finally {
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
        }
 
    }
 
    public static String changeFileFormat(String flow) {
        BigDecimal flows = new BigDecimal(flow);
        if (flows.compareTo(new BigDecimal(0)) > 0 && flows.compareTo(new BigDecimal(1024)) < 0) {//小于1M
            return flows.toString() + "B";
        } else if(flows.compareTo(new BigDecimal(1024)) >= 0 && flows.compareTo(new BigDecimal(1048576)) < 0){
            BigDecimal result = flows.divide(new BigDecimal(1024),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "KB";
        } else if(flows.compareTo(new BigDecimal(1048576)) >= 0 && flows.compareTo(new BigDecimal(1073741824)) < 0){
            BigDecimal result = flows.divide(new BigDecimal(1048576),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "MB";
        } else if(flows.compareTo(new BigDecimal(1073741824)) >= 0 && flows.compareTo(new BigDecimal("1099511627776")) < 0){
            BigDecimal result = flows.divide(new BigDecimal(1073741824),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "GB";
        } else if(flows.compareTo(new BigDecimal("1099511627776")) >= 0 && flows.compareTo(new BigDecimal("1125899906842624")) < 0){
            BigDecimal result = flows.divide(new BigDecimal("1099511627776"),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "TB";
        } else if(flows.compareTo(new BigDecimal("1125899906842624")) >= 0){
            BigDecimal result = flows.divide(new BigDecimal("1125899906842624"),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "PB";
        }else {
            return "0";
        }
    }
 
 
    public static String changeFileFormatKb(String flow) {
        BigDecimal flows = new BigDecimal(flow);
        if (flows.compareTo(new BigDecimal(0)) > 0 && flows.compareTo(new BigDecimal(1024)) < 0) {//小于1M
            return flows.toString() + "B";
        } else if(flows.compareTo(new BigDecimal(1024)) >= 0){
            BigDecimal result = flows.divide(new BigDecimal(1024),2,BigDecimal.ROUND_HALF_UP);
            return  result.toString() + "KB";
        } else {
            return "0";
        }
    }
 
 
}