liurunyu
2024-10-21 a408f749e77f3c13834770727dc2e1e6aafe062c
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
package com.dy.pipIrrWebFile.fm;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 分布式web文件系统管理系统
 */
@Slf4j
@RestController
@RequestMapping(path="fm")
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked")
public class FmCtrl {
 
    /**
     * 生成文件名,并且计算出该文件名对应的文件服务器属性
     * 文件名称是不带路径的名称,例如:20170818153254_100000007.jpg,文件名中不能有“/”或“\”字符
     * @param fileExtName 文件扩展名
     * @return
     */
    @PostMapping(path = "create")
    public FileRestVo create(String fileExtName){
        FileRestVo rvo = new FileRestVo() ;
        if(fileExtName != null && !fileExtName.trim().equals("")){
            try {
                String id = new FileNameIdUtil().generate();
                if(id != null){
                    rvo.fileName = FileName.createFileName(false, id, fileExtName) ;
                    rvo = new RestHashDeal().fileTransRest(rvo.fileName, rvo) ;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rvo ;
    }
 
    /**
     * 解析文件名(带或不带相对路径),计算出该文件名对应的文件服务器属性
     * 文件名称可以是不带路径的名称,例如:20170818153254_100000007.jpg
     * 文件名称可以是带路径的名称,例如:webFile/photo/20170818153254_100000007.jpg
     * @param filePath
     * @return
     */
    @PostMapping(path = "parsePath")
    public FileRestVo parsePath(String filePath){
        FileRestVo rvo = new FileRestVo() ;
        if(filePath != null && !filePath.trim().equals("")){
            try {
                int index = filePath.lastIndexOf("\\") ;
                if(index > 0){
                    filePath = filePath.substring(index + 1);
                }
                index = filePath.lastIndexOf("/") ;
                if(index > 0){
                    filePath = filePath.substring(index + 1);
                }
                index = filePath.lastIndexOf("?") ;
                if(index > 0){
                    filePath = filePath.substring(0, index);
                }
                rvo.fileName = filePath ;
                rvo = new RestHashDeal().fileTransRest(filePath, rvo) ;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rvo ;
    }
    /**
     * 解析文件名,并且计算出该文件名对应的文件服务器属性
     * 文件名称可以是不带路径的名称,例如:20170818153254_100000007.jpg
     * 文件名称可以是带路径的名称,例如:webFile/photo/20170818153254_100000007.jpg
     * @param filePaths
     * @return
     */
    @PostMapping(path = "parsePathList", consumes = MediaType.APPLICATION_JSON_VALUE)//前端提交json数据
    public FileRestVo[] parsePathList(List<String> filePaths){
        List<FileRestVo> rList = new ArrayList<FileRestVo>() ;
        if(filePaths != null && filePaths.size() > 0){
            try {
                for(String filePath : filePaths){
                    FileRestVo rvo = new FileRestVo() ;
                    int index = filePath.lastIndexOf("\\") ;
                    if(index > 0){
                        filePath = filePath.substring(index + 1);
                    }
                    index = filePath.lastIndexOf("/") ;
                    if(index > 0){
                        filePath = filePath.substring(index + 1);
                    }
                    index = filePath.lastIndexOf("?") ;
                    if(index > 0){
                        filePath = filePath.substring(0, index);
                    }
                    rvo.fileName = filePath ;
                    rvo = new RestHashDeal().fileTransRest(filePath, rvo) ;
                    rList.add(rvo) ;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rList.toArray(new FileRestVo[0]) ;
    }
 
    /**
     * 解析哈希值,计算出该哈希值对应的文件服务器属性
     * @param hashCode
     * @return
     */
    @PostMapping(path = "parseHashcode")
    public FileRestVo parseHashcode(Integer hashCode){
        FileRestVo rvo = new FileRestVo() ;
        if(hashCode != null){
            try {
                rvo.fileName = null ;
                rvo = new RestHashDeal().fileTransRest(hashCode, rvo) ;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rvo ;
    }
 
 
}