wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
package com.vci.client.uif.actions.client;
 
import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
 
import com.vci.client.fm.ClientFileObjectOperation;
import com.vci.client.fm.FileObject;
 
/**
 * 文件上传下载工具
 * @author VCI-STGK006
 *
 */
public class FileOperation {
 
    /**
     * 文件对象操作方法
     */
    private ClientFileObjectOperation operation = new ClientFileObjectOperation();
    
    /**
     * 构造器
     */
    public FileOperation(){}
    
    /**
     * 选择并上传文件
     * @param parent 父组件
     * @param currentDirectory 默认目录
     * @param filters 文件过滤器
     * @param multiSelection 是否可以多选 true允许
     * @param revisionLimit 版本上限? 
     * @param boOid  业务对象ID
     * @return FileObject[] 业务对象集合 如果没有上传任何文件时为null
     * @throws Exception
     */
    public FileObject[] selectUploadFile(Component parent, String currentDirectory,FileFilter[] filters, boolean multiSelection, int revisionLimit, String boOid) throws Exception {
        //创建文件选择窗口
        JFileChooser jfc = null;
        if(currentDirectory == null || currentDirectory.equals("")){
            jfc = new JFileChooser();
        } else {
            jfc = new JFileChooser(currentDirectory);
        }
        jfc.setDialogTitle("请选择文件");
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setMultiSelectionEnabled(multiSelection);
        //设置文件过滤器
        if(filters != null){
            for(FileFilter filter : filters){
                jfc.addChoosableFileFilter(filter);
            }
        }
        //FileFilter DefaultFilter = new FileNameExtensionFilter("所有文件", "*");
        //jfc.addChoosableFileFilter(DefaultFilter);
        int result = jfc.showOpenDialog(parent);
        if(result == JFileChooser.APPROVE_OPTION){
            //获得选择文件
            File[] files = null;
            if(multiSelection){
                files = jfc.getSelectedFiles();
            } else {
                files = new File[1];
                files[0] = jfc.getSelectedFile();;
            }
            return uploadFile(files, revisionLimit, boOid);
        }
        return null;
    }
    
    /**
     * 上传文件
     * @param filePath 文件路径
     * @param revisionLimit 版本上限?
     * @param boOid 业务对象ID
     * @return 文件对象
     * @throws Exception 异常类型 Exception VCIError
     */
    public FileObject uploadFile(String filePath, int revisionLimit, String boOid) throws Exception{
        if(filePath == null || filePath.trim().equals("")){
            throw new Exception("参数错误:文件路径为空!");
        }
        String[] filePaths = {filePath};
        FileObject[] fileObjects = uploadFile(filePaths, revisionLimit, boOid);
        if(fileObjects != null && fileObjects.length == 1){
            return fileObjects[0];
        }
        return null;
    }
    
    /**
     * 上传文件
     * @param file 文件
     * @param revisionLimit 版本上限?
     * @param boOid 业务对象ID
     * @return 文件对象
     * @throws Exception 异常类型 Exception VCIError
     */
    public FileObject uploadFile(File file, int revisionLimit, String boOid) throws Exception{
        if(file == null){
            throw new Exception("参数错误:文件对象为空!");
        }
        File[] files = new File[1];
        files[0] = file;
        FileObject[] fileObjects = uploadFile(files, revisionLimit, boOid);
        if(fileObjects != null && fileObjects.length == 1){
            return fileObjects[0];
        }
        return null;
    }
 
    /**
     * 上传文件
     * @param filePaths 文件路径集合
     * @param revisionLimit 版本上限?
     * @param boOid 业务对象ID
     * @return 文件对象
     * @throws Exception 异常类型 Exception VCIError
     */
    public FileObject[] uploadFile(String[] filePaths, int revisionLimit, String boOid) throws Exception{
        FileObject[] fileObjects = null;
        if(filePaths == null || filePaths.length<1){
            throw new Exception("参数错误:文件路径为空!");
        }
        fileObjects = new FileObject[filePaths.length];
        for(int i = 0; i<filePaths.length; i++){
            //验证文件是否存在或是否是标准文件
            String filePath = filePaths[i];
            File newFile = new File(filePath);
            if(!newFile.exists()){
                throw new Exception("文件不存在!");
            }
            if(!newFile.isFile()){
                throw new Exception("不是标准文件!");
            }
            FileObject fileObject = operation.createNewFile();
            //得到文件名称和文件类型
            String fileName = newFile.getName();
            String fileRealName = "";
            String fileType = "";
            int index = fileName.lastIndexOf(".");
            if(index<1){
                fileRealName = fileName;
                fileType = "";
            } else {
                fileRealName = fileName.substring(0, index - 1);
                fileType = fileName.substring(index);
            }
            //设置文件对象基本信息
            fileObject.setName(fileName);
            fileObject.setFileType(fileType);
            fileObject.setRevisionLimit(revisionLimit);
            fileObject.setDocumentoid(boOid);
            String localfilePath = filePath;
            //保存文件及文件对象
            fileObjects[i] = operation.createNewFile(localfilePath, fileObject);
        }
        return fileObjects;
    }
    
    /**
     * 上传文件
     * @param files 文件集合
     * @param revisionLimit 版本上限?
     * @param boOid 业务对象ID
     * @return 文件对象
     * @throws Exception 异常类型 Exception VCIError
     */
    public FileObject[] uploadFile(File[] files, int revisionLimit, String boOid) throws Exception{
        if(files == null || files.length < 1){
            throw new Exception("参数错误:文件对象为空!");
        }
        FileObject[] fileObjects = new FileObject[files.length];
        for(int i = 0; i<files.length; i++){
            //验证文件是否存在或是否是标准文件
            File newFile = files[i];
            if(!newFile.exists()){
                throw new Exception("文件不存在!");
            }
            if(!newFile.isFile()){
                throw new Exception("不是标准文件!");
            }
            FileObject fileObject = createFileObject(newFile, revisionLimit, boOid);
            String localfilePath = newFile.getPath();
            //保存文件及文件对象
            fileObjects[i] = operation.createNewFile(localfilePath, fileObject);
        }
        return fileObjects;
    }
    
    /**
     * 创建FileObject对象
     * @param newFile 文件对象
     * @param revisionLimit 版本上线
     * @param boOid fromBO对象
     * @return
     */
    public FileObject createFileObject(File newFile, int revisionLimit, String boOid) throws Exception{
        FileObject fileObject = operation.createNewFile();;
        //得到文件名称和文件类型
        String fileName = newFile.getName();
        String fileRealName = "";
        String fileType = "";
        int index = fileName.lastIndexOf(".");
        if(index<1){
            fileRealName = fileName;
            fileType = "";
        } else {
            fileRealName = fileName.substring(0, index - 1);
            fileType = fileName.substring(index);
        }
        //设置文件对象基本信息
        fileObject.setName(fileName);
        fileObject.setFileType(fileType);
        fileObject.setRevisionLimit(revisionLimit);
        fileObject.setDocumentoid(boOid);
        return fileObject;
    }
    
    /**
     * 选择保存路径并下载文件到本地
     * @param parent 父组件
     * @param foOids 文件对象OID
     * @param currentDirectory 默认打开路径
     * @return FileObject[]下载的文件对象
     * @throws Exception 异常类型 Exception VCIError
     */
    public FileObject[] downloadFile(Component parent, String[] foOids, String currentDirectory) throws Exception{
        //创建文件选择窗口
        JFileChooser jfc = null;
        if(currentDirectory == null || currentDirectory.equals("")){
            jfc = new JFileChooser();
        } else {
            jfc = new JFileChooser(currentDirectory);
        }
        jfc.setDialogTitle("请选择保存路径");
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.setDialogType(JFileChooser.SAVE_DIALOG);
        jfc.setMultiSelectionEnabled(false);
        int result = jfc.showSaveDialog(parent);
        if(result == JFileChooser.APPROVE_OPTION){
            File file = jfc.getSelectedFile();
            //下载保存文件
            return downloadFile(foOids, file.getPath());
        }
        return null;
    }
    
    /**
     * 打开文件
     * 文件加载到系统临时目录下
     * @param foOids 文件对象OID
     * @throws Exception
     */
    public boolean openFile(String[] foOids)throws Exception{
        String tempFile = System.getProperty("java.io.tmpdir") 
                + File.separator + "innovation";
        //每次下载之前都清空之前的文件,无论是否删除成功都执行下一步操作
        File newFile = new File(tempFile);
        try{
            if(newFile.exists()){
                newFile.delete();
            }
        } catch(Exception e){
        }
        return openFile(foOids, tempFile);
    }
    
    /**
     * 打开文件
     * @param foOids 文件对象OID
     * @param tempPath 文件保存路径
     * @throws Exception
     */
    public boolean openFile(String[] foOids, String tempPath) throws Exception{
        //校验路径是否存在
        File tempFile = new File(tempPath);
        if(!tempFile.exists()){
            tempFile.mkdirs();
        }
        //下载文件
        FileObject[] fos = downloadFile(foOids, tempPath);
        //打开文件
        if(fos != null && fos.length > 0){
            for(int i = 0; i < fos.length; i++){
                String[] cmdArray = {"cmd.exe","/c",tempPath 
                        + File.separator + fos[i].getBusinessObject().name};
                Runtime.getRuntime().exec(cmdArray);
            }
            return true;
        }
        return false;
    }
    
    /**
     * 下载文件到本地
     * @param foOids 文件对象OID
     * @param savePath 文件保存路径
     * @return FileObject[]下载的文件对象
     * @throws Exception 异常类型 Exception VCIError
     */
    public FileObject[] downloadFile(String[] foOids, String savePath) throws Exception{
        if(foOids != null && foOids.length >0){
            FileObject[] fileObjects = new FileObject[foOids.length];
            for(int i = 0; i<foOids.length; i++){
                //下载文件对象
                String foOid = foOids[i];
                FileObject fileObject = operation.getFileObject(foOid);
                if(fileObject == null || fileObject.getBusinessObject() == null
                        || fileObject.getBusinessObject().oid == null 
                        || fileObject.getBusinessObject().oid.equals("")) {
                    fileObjects[i] = null;
                    continue;
                }
                //下载文件到本地文件夹
                String fileName = fileObject.getName(); //+ "." + fileObject.getFileType();
                String fileSavePath = savePath + File.separator + fileName;
                try {
                    if(!operation.downLoadFileByOid(fileSavePath, fileObject.getOid())){
                        throw new Exception("文件“"+ fileName +"”加载失败!");
                    }
                    
//                    if(!operation.downLoadFile(fileSavePath, fileObject.getFilePath())){
//                        throw new Exception("文件“"+ fileName +"”加载失败!");
//                    }
                } catch (Exception e) {
                    throw new Exception("文件“"+ fileName +"”加载失败!请检查文件服务是否开启或文件柜是否设置!");
                }
                fileObjects[i] = fileObject;
            }
            return fileObjects;
        }
        return null;
    }
}