whyclxw
2025-02-27 5636f3f0e6005cc30dca08ce33f5e6f1e7cd457f
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.whyc.constant.UserOperation;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.mapper.SoftcodeMapper;
import com.whyc.mapper.SoftwareMapper;
import com.whyc.pojo.Softcode;
import com.whyc.pojo.Software;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class SoftcodeService {
 
    @Autowired(required = false)
    private SoftcodeMapper mapper;
 
    @Autowired
    private DocLogService logService;
 
 
    //源码上传
    public Response uploadCode(MultipartFile file, String fileNames) throws IOException {
        String[] nameList=fileNames.split(",");
        String codeName = file.getOriginalFilename();
        Date date = new Date();
        String dateUnion = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(date);
        //存储路径
        String rootFile = CommonUtil.getRootFile();
        String softcodeDir = rootFile + "software" + File.separator +"code"+ File.separator + dateUnion;
        String softcodeHttpUrl = softcodeDir.substring(softcodeDir.lastIndexOf("doc_file"+ File.separator + "software"));
        File softwareDirFile = new File(softcodeDir);
        if(!softwareDirFile.exists()){
            softwareDirFile.mkdirs();
        }
        String codeUrl=softcodeDir+File.separator+codeName;
        file.transferTo(new File(codeUrl));
        if(nameList.length>0){
            for(String name:nameList){
                Softcode softcode=new Softcode();
                softcode.setFileName(name);
                softcode.setCreateTime(new Date());
                softcode.setCodeUrl(softcodeHttpUrl+File.separator+codeName);
                softcode.setCodeName(codeName.substring(0,codeName.lastIndexOf(".")));
                //判断是否存在源码,存在修改,不存在添加
                if(mapper.selectOne(new QueryWrapper<Softcode>().eq("file_name",name))!=null){
                    mapper.update(softcode,new UpdateWrapper<Softcode>().eq("file_name",name));
                }else{
                    mapper.insert(softcode);
                }
            }
        }
        return new Response().set(1,true,"源码上传");
    }
 
 
    //根据软件id实现源码下载
    public void downLoadCode(HttpServletRequest req, HttpServletResponse resp, String fileName) {
        String fileDirName = FileDirPath.getFileDirName();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("file_name",fileName);
        wrapper.last("limit 1");
        Softcode softcode=mapper.selectOne(wrapper);
 
        String name=softcode.getCodeUrl().substring(softcode.getCodeUrl().lastIndexOf("\\")+1);
        try {
            // 转码防止乱码
            //resp.addHeader("Content-Disposition", "attachment;filename=" + new String(softwareName.getBytes("UTF-8"), "ISO8859-1"));
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode (name, "utf-8"));
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(fileDirName+ File.separator+softcode.getCodeUrl());
            int len=0;
            byte[] buffer =new byte[1024];
            //7. 将缓冲区中的数据输出
            while ((len=in.read(buffer))>0){
                out.write(buffer,0,len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //记录日志
        logService.recordOperationLogDownLoad(ActionUtil.getUser().getId(),ActionUtil.getUser().getName(), UserOperation.TYPE_DOWNLOAD_SOFWARE.getType(),new Date(),req.getRemoteAddr()
                ,name,fileDirName+File.separator+softcode.getCodeUrl(),"",name,"");
    }
}