lxw
2022-08-17 0dbef7383f2f2acbd77a5d5b9f4580143a58aa37
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.UserOperation;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.dto.ZipUtils;
import com.whyc.mapper.ProductSoftwareMapper;
import com.whyc.pojo.DocUser;
import com.whyc.pojo.ProductSoftware;
import com.whyc.util.ActionUtil;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;
 
@Service
public class ProductSoftwareService {
 
    @Autowired(required = false)
    private ProductSoftwareMapper mapper;
 
    @Autowired
    private DocLogService logService;
    //查询软件的信息
    public Response getAllSoftware(String parentModel, String softwareName,int pageCurr,int pageSize) {
        PageHelper.startPage(pageCurr,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        if(parentModel!=null&&!parentModel.isEmpty()){
            wrapper.like("parent_model",parentModel);
        }
        if(softwareName!=null&&!softwareName.isEmpty()){
            wrapper.like("software_name",softwareName);
        }
        wrapper.orderByAsc("id");
        List list=mapper.selectList(wrapper);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0?true:false,pageInfo,"返回数据");
    }
    //根据软件名称实现软件下载
    public void downLoadSoftware(HttpServletRequest req, HttpServletResponse resp, String softwareName) {
        String fileDirName = FileDirPath.getFileDirName();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("software_name",softwareName);
        wrapper.last("limit 1");
        ProductSoftware software=mapper.selectOne(wrapper);
        try {
            String userAgent = req.getHeader("User-Agent");
            // 针对IE或者以IE为内核的浏览器:
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                softwareName = java.net.URLEncoder.encode(softwareName, "UTF-8");
            } else {
                // 非IE浏览器的处理:
                softwareName = new String(softwareName.getBytes("UTF-8"), "ISO-8859-1");
            }
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + softwareName);
            resp.setCharacterEncoding("UTF-8");
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(fileDirName+File.separator+software.getSoftwareUrl());
            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();
        }
        //记录日志
        DocUser docUser= ActionUtil.getUser();
        String operationDetail="具体软件名称为:"+softwareName;
        String opreationMsg="执行了软件下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
    }
}