zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
package com.shlanbao.tzsc.pms.file.docfile.service.impl;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.shlanbao.tzsc.utils.tools.*;
import org.apache.poi.common.usermodel.Fill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.shlanbao.tzsc.base.dao.DocFileDaoI;
import com.shlanbao.tzsc.base.mapping.DocFile;
import com.shlanbao.tzsc.base.mapping.SysUser;
import com.shlanbao.tzsc.base.model.SessionInfo;
import com.shlanbao.tzsc.base.service.BaseService;
import com.shlanbao.tzsc.pms.file.docfile.beans.DocFileBean;
import com.shlanbao.tzsc.pms.file.docfile.service.DocFileServiceI;
 
@Service
public class DocFileServiceImpl extends BaseService implements DocFileServiceI {
    @Autowired
    private DocFileDaoI fileDaoI;
    @Autowired
    private HttpServletRequest request;
 
    private File sourceFile;  //目标源文件
    private File pdfFile;     //PDF目标文件
    private File swfFile;     //swf目标文件
    private Runtime runtime;
    private static final ResourceBundle bundle = ResourceBundle.getBundle("config");
    /**
     *查询所有文件
     */
    @Override
    public List<DocFileBean> getDocFileAll(DocFileBean fileBean)
            throws Exception {
        List<DocFileBean> df = new ArrayList<DocFileBean>();
 
        StringBuilder hql = new StringBuilder();
        hql.append("from DocFile d  where 1=1 and d.attr=0 order by d.createTime");
        List<DocFile> docFiles = fileDaoI.query(hql.toString());
        if(docFiles!=null){
            for (DocFile docfile : docFiles) {
 
                DocFileBean dffile = new DocFileBean();
                dffile = BeanConvertor.copyProperties(docfile, DocFileBean.class);
                if (docfile.getDocFile() != null) {
                    dffile.setPid(docfile.getDocFile().getId());
                }
                dffile.setSysUserName(docfile.getSysUser().getName());
                if (dffile.getType().equals("1")) {
                    dffile.setIconCls("icon-standard-folder");
                } else {
                    switch (dffile.getFileType()){
                        case "doc" :
                            dffile.setIconCls("icon-standard-page-white-word");
                            break;
                        case "docx" :
                            dffile.setIconCls("icon-standard-page-white-word");
                            break;
                        case "xls" :
                            dffile.setIconCls("icon-standard-page-white-excel");
                            break;
                        case "xlsx" :
                            dffile.setIconCls("icon-standard-page-white-excel");
                            break;
                        case "pdf" :
                            dffile.setIconCls("icon-standard-page-white-acrobat");
                            break;
                        case "png" :
                            dffile.setIconCls("icon-standard-picture");
                            break;
                        case "jpg" :
                            dffile.setIconCls("icon-strandard-picture");
                            break;
                        case "gif" :
                            dffile.setIconCls("icon-standard-picture");
                            break;
                        case "zip" :
                            dffile.setIconCls("icon-standard-page-white-zip");
                            break;
                        case "rar" :
                            dffile.setIconCls("icon-standard-page-white-zip");
                            break;
                        case "ppt" :
                            dffile.setIconCls("icon-standard-page-white-powerpoint");
                            break;
 
                        case "pptx" :
                            dffile.setIconCls("icon-standard-page-white-powerpoint");
                            break;
                        default:
                            dffile.setIconCls("icon-standard-page-white-text");
                            break;
                    }
                }
                dffile.setUrl(bundle.getString("service_url") + dffile.getUrl());
                FillUserInfoUtil.fillCreateAndUpdateUserInfo(docfile,dffile);
                df.add(dffile);
            }
        }
        return df;
    }
 
    @Override
    public void deleteDocFile(String Id) {
 
        fileDaoI.deleteById(Id, DocFile.class);
    }
 
    /**
     * 文件上传
     * 修改:shisihai
     */
    @LogAnno(operateType = "文件上传")
    @Override
    public String uploadDocFile(String parentId, HttpServletRequest request,HttpSession session) throws Exception {
        request.setCharacterEncoding("utf-8");
        SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
        String[] UrlfileNames=(String[]) request.getParameterValues("urlfileName")[0].split(",");
        String err = null;
        // 获取文件名
        String[] fileUrls = (String[]) request.getParameterValues("fileUrl")[0].split(",");
        String[] fileNames = (String[]) request.getParameterValues("fileName")[0].split(",");
        // 取到 pid
        DocFile dbdocFile = fileDaoI.findById(DocFile.class, parentId);
        String pid;
        if (dbdocFile.getType().equals("2")) {
            // 表示是文件
            pid = dbdocFile.getDocFile().getId();
        } else {
            // 文件夹
            pid = dbdocFile.getId();
        }
        for (int i = 0; i < fileUrls.length; i++) {
            if (this.DocFileByFileName(fileNames[i].substring(0,fileNames[i].lastIndexOf(".")))) {
                DocFile docFile = new DocFile();
                docFile.setDocFile(new DocFile(pid));
                docFile.setCreateTime(new Date());
                docFile.setFilename(fileNames[i].substring(0,fileNames[i].lastIndexOf(".")));
                docFile.setFileType(fileNames[i].substring(fileNames[i].lastIndexOf(".") + 1));
                docFile.setType("2");
                docFile.setUrl(fileUrls[i].replace(docFile.getFilename(),UrlfileNames[i]));//修改后,
                //docFile.setUrl(bundle.getString("upload")+"/"+UrlfileNames[i]+"."+fileUrls[i].substring(fileUrls[i].lastIndexOf(".") + 1));
                docFile.setAttr("0");
                docFile.setSysUser(new SysUser(sessionInfo.getUser().getId()));
                FillUserInfoUtil.fillCreateUserInfo(docFile,request);
                fileDaoI.save(docFile);
            } else {
                err = "此文件已经存在!";
            }
        }
        return err;
    }
    /*public void init( HttpServletRequest request) {
        File uploadPath = new File(request.getServletContext().getRealPath(bundle.getString("upload")));
        File previewPDF = new File(request.getServletContext().getRealPath(bundle.getString("pdf")));
        if (!uploadPath.exists()) {
            uploadPath.mkdir();
        }
        if (!previewPDF.exists()) {
            previewPDF.mkdir();
        }
    }*/
    /**
     * file初始化
     * @param sourceFilePath  源文件
     * @param pdfPath pdf file
     * @param swfPath swf file
     */
    protected void initFilePath(String sourceFilePath,String pdfPath,String swfPath){
        //判断源文件是否为PDF文件
        if("pdf".equals(sourceFilePath.substring((sourceFilePath.lastIndexOf(".")+1)))){
            sourceFile = new File(sourceFilePath);
            pdfFile = new File(pdfPath);
            swfFile = new File(swfPath);
        }else{
            sourceFile = new File(sourceFilePath);
            pdfFile = new File(pdfPath);
            swfFile = new File(swfPath);
        }
        System.out.println("文件对象已生成,准备转换..");
    }
    /**
     * 文件转换为swf格式
     * @throws IOException
     */
    protected void fileConvert() throws IOException{
        System.out.println(pdfFile.getName());
        if(sourceFile.exists()){
            if(!pdfFile.exists()){
                OpenOfficeConnection connection = null;
                try {
                    connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
                    connection.connect();
                    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                    converter.convert(sourceFile, pdfFile);//源文件转换为PDF
                    pdfFile.createNewFile();
                    connection.disconnect();
                    System.out.println("转换为PDF文件:"+pdfFile.getPath());
                } catch (ConnectException e) {
                    System.out.println("openOffice 服务未启动: ");
                    e.printStackTrace();
                    throw e;
                } catch (OpenOfficeException e){
                    System.out.println("文件读取失败: ");
                    e.printStackTrace();
                    throw e;
                } catch (Exception e){
                    System.out.println("未知异常:");
                    e.printStackTrace();
                }finally{//add by luther.zhang 20150129防止报错后不释放链接
                    if(connection!=null){
                        connection.disconnect();
                    }
                }
        } else {
            System.out.println("Pdf文件已经转换,无需再次转。");
        }
        }else{
            System.out.println("要转换的源文件不存在, 可能是路径错误。");
        }
 
        runtime = Runtime.getRuntime();
 
        new Thread(){
 
            @Override
            public void run(){
                if(!swfFile.exists()){
                    if(pdfFile.exists()){
                        try {
                            //System.out.println(ConfigParameter.psPath +
                            //pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
                            //Process p = runtime.exec(ConfigParameter.psPath + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");//单独的进程中执行指定的字符串命令
                            //p.waitFor();//当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止
 
                            //runtime.exec(ConfigParameter.psPath + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");//单独的进程中执行指定的字符串命令
                            String command=bundle.getString("exePath")+" -t \""+pdfFile.getPath()+"\" -o \""
                                    +swfFile.getPath()+"\" -T 9";
                            System.out.println(command);
                            runtime.exec(command);//单独的进程中执行指定的字符串命令
 
                            swfFile.createNewFile();
 
                            System.out.println("swfFile 格式路径:"+swfFile.getPath());
 
                            System.out.println("swfFile 格式名称:"+swfFile.getName());
 
                        } catch (IOException e) {
                            System.out.println("IOException:");
                            e.printStackTrace();
                        } /*catch (InterruptedException e) {
                            System.out.println("InterruptedException:");
                            e.printStackTrace();
                        }*/
 
                    } else {
                        System.out.println("Pdf文件格式不存在,无法转换");
                    }
                } else {
                    System.out.println("已转换为SWF格式,无需再次转换!");
                }
            }
 
        }.start();
    }
    @Override
    public DocFile DocFileById(String Id) throws Exception {
 
        return fileDaoI.findById(DocFile.class, Id);
    }
 
    /**
     * 文件下载
     *
     * @param filePath
     *            文件路径
     * @param fileName
     *            文件名称
     * @param response
     *            响应对象
     * @return
     */
    public boolean download(String filePath, String fileName,
            HttpServletResponse response) {
 
        // 将服务器上的文件写入本地文件
        OutputStream outputStream = null;
        // 读取服务器上的文件信息
        InputStream inputStream = null;
        // 文件名,输出到用户的下载对话框
        // String fileName = null;
        try {
 
            inputStream = new FileInputStream(filePath);
            response.setContentType("text/plain");
            response.setHeader("Location", fileName);
            response.setHeader("Content-Disposition", "attachment;filename="
                    + fileName);
            outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int i = -1;
            while ((i = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, i);
            }
 
        } catch (Exception e) {
            log.error("下载失败", e);
            return false;
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                log.error("关闭流失败", e);
            }
        }
        return true;
    }
    @LogAnno(operateType = "删除文档")
    @Override
    public String deleteFile(String id) throws Exception {
        // 判断有没有子节点
        DocFile docFile = fileDaoI.findById(DocFile.class, id);
        Set<DocFile> docFiles = docFile.getDocFiles();
        String err = null;
        int str=0;
        for (DocFile docFile2 : docFiles) {
            if(docFile2.getAttr().equals("0")){
                str++;
            }
        }
        if (str > 0) {
            err = "请先删除文件夹里的文件";
        } else {
            docFile.setAttr("1");
        }
        return err;
    }
    /**
     * 添加文件夹
     *
     */
    @LogAnno(operateType = "添加文件夹")
    @Override
    public String saveFile(String parentId, String filename,
            HttpServletRequest request, HttpSession session) throws Exception {
        request.setCharacterEncoding("utf-8");
        SessionInfo sessionInfo = (SessionInfo) session
                .getAttribute("sessionInfo");
        String err = null;
        if (this.fileByFileName(filename,parentId)&& StringUtil.notNull(filename)) {
            String pid;
            String url;
            // 取到 pid
            if(StringUtil.notNull(parentId)){
                DocFile dbdocFile = fileDaoI.findById(DocFile.class, parentId);
 
                if (dbdocFile.getType().equals("2")) {
                    // 表示是文件
                    pid = dbdocFile.getDocFile().getId();
                    url = dbdocFile.getDocFile().getUrl();
                } else {
                    // 文件夹
                    pid = dbdocFile.getId();
                    url = dbdocFile.getUrl();
                }
            }else{
                pid=null;
                url ="upload";
 
            }
            System.out.println(request.getServletContext()
                    .getRealPath("upload"));
 
            DocFile docFile = new DocFile();
            if(StringUtil.notNull(pid)){
            docFile.setDocFile(new DocFile(pid));
            }else{
                docFile.setDocFile(null);
            }
            // docFile.setCreateTime(new Date());
            docFile.setFilename(filename);
            docFile.setFileType(null);
            docFile.setType("1");
            docFile.setUrl(url);
            docFile.setAttr("0");
            docFile.setCreateTime(new Date());
            docFile.setSysUser(new SysUser(sessionInfo.getUser().getId()));
            FillUserInfoUtil.fillCreateUserInfo(docFile,request);
            fileDaoI.save(docFile);
 
        } else {
            err = "此文件已经存在、或者不能为空";
 
        }
        return err;
    }
 
    @Override
    public boolean DocFileByFileName(String fileName) {
        List<DocFile> docFiles = fileDaoI.query(
                "from DocFile d where d.filename=?  and d.attr='0'", fileName);
        if (docFiles.size() > 0) {
            return false;
        } else {
            return true;
        }
    }
 
    public boolean fileByFileName(String fileName,String pid) {
        List<DocFile> docFiles = fileDaoI.query(
                "from DocFile d where d.filename=? and type = 1 and pid = ?  and d.attr='0'", fileName,pid);
        if (docFiles.size() > 0) {
            return false;
        } else {
            return true;
        }
    }
    @LogAnno(operateType = "修改文件夹或文件名字")
    @Override
    public String updatefile(DocFileBean docFileBean, HttpSession session) {
        String err = null;
        SessionInfo sessionInfo = (SessionInfo) session
                .getAttribute("sessionInfo");
        if (sessionInfo.getUser() == null) {
            err = "请登录后修改";
        } else {
        DocFile file=    fileDaoI.findById(DocFile.class, docFileBean.getId());
            file.setFilename(docFileBean.getFilename());
            file.setSysUser( new SysUser(sessionInfo.getUser().getId()));
            FillUserInfoUtil.fillUpdateUserInfo(file,request);
        }
        return err;
    }
 
 
 
}