lxw
2022-09-07 724f785c969715b2481326ba7605d712ceccf790
软件下载
3个文件已修改
69 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/SoftwareController.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/Software.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/SoftwareService.java 52 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/SoftwareController.java
@@ -10,6 +10,8 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
@@ -56,5 +58,10 @@
        return service.getSoftBySubCode(subCode);
    }
    @ApiOperation(value = "根据软件id实现软件下载")
    @GetMapping("downLoadSoftware")
    public void downLoadSoftware(HttpServletRequest req, HttpServletResponse resp, @RequestParam int id){
        service.downLoadSoftware(req,resp,id);
    }
}
src/main/java/com/whyc/pojo/Software.java
@@ -12,7 +12,9 @@
    private Integer id;
    private String fileName;
    private String fileUrl;
    @ApiModelProperty("软件类型")
    private String type;
@@ -54,6 +56,14 @@
        this.fileName = fileName;
    }
    public String getFileUrl() {
        return fileUrl;
    }
    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }
    public String getType() {
        return type;
    }
src/main/java/com/whyc/service/SoftwareService.java
@@ -2,9 +2,14 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.whyc.constant.UserOperation;
import com.whyc.dto.FileDirPath;
import com.whyc.dto.Response;
import com.whyc.mapper.SoftwareMapper;
import com.whyc.pojo.DocUser;
import com.whyc.pojo.ProductSoftware;
import com.whyc.pojo.Software;
import com.whyc.util.ActionUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -13,8 +18,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
@@ -23,8 +30,11 @@
@Service
public class SoftwareService {
    @Autowired
    @Autowired(required = false)
    private SoftwareMapper mapper;
    @Autowired
    private DocLogService logService;
    public List<Software> excelParse(InputStream inputStream) throws IOException, InvalidFormatException, ParseException {
        List<Software> softwareList = new LinkedList<>();
@@ -95,4 +105,40 @@
        List list=mapper.selectList(wrapper);
        return new Response().setII(1,list.size()>0,list,"软件信息返回");
    }
    //根据软件名称实现软件下载
    public void downLoadSoftware(HttpServletRequest req, HttpServletResponse resp, int id) {
        String fileDirName = FileDirPath.getFileDirName();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        wrapper.last("limit 1");
        Software software=mapper.selectOne(wrapper);
        try {
            // 转码防止乱码
            //resp.addHeader("Content-Disposition", "attachment;filename=" + new String(softwareName.getBytes("UTF-8"), "ISO8859-1"));
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( software.getFileName(), "utf-8"));
            OutputStream out = resp.getOutputStream();
            FileInputStream in = new FileInputStream(fileDirName+File.separator+software.getFileUrl());
            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="具体软件名称为:"+software.getFileName();
        String opreationMsg="执行了软件下载操作";
        String terminalIp=req.getRemoteAddr();
        logService.recordOperationLog(docUser.getId(),docUser.getName(), UserOperation.TYPE_DOWNLOAD.getType(),new Date(),terminalIp,opreationMsg,operationDetail);
    }
}