whyclxw
4 天以前 bfa320956f20988fe671b0c4d25aa82fe766a98d
src/main/java/com/whyc/service/ProductSoftwareService.java
@@ -3,11 +3,25 @@
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.net.URLEncoder;
import java.util.Date;
import java.util.List;
@Service
@@ -15,6 +29,9 @@
    @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);
@@ -28,6 +45,50 @@
        wrapper.orderByAsc("id");
        List list=mapper.selectList(wrapper);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list.size()>0?true:false,pageInfo,"返回数据");
        return new Response().setII(1,list.size()>0,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 {
            // 转码防止乱码
            //resp.addHeader("Content-Disposition", "attachment;filename=" + new String(softwareName.getBytes("UTF-8"), "ISO8859-1"));
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( softwareName, "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();
        }
        //记录日志
        logService.recordOperationLogDownLoad(ActionUtil.getUser().getId(),ActionUtil.getUser().getName(), UserOperation.TYPE_DOWNLOAD_SOFWARE.getType(),new Date(),req.getRemoteAddr(),
                software.getSoftwareUrl(),fileDirName+File.separator+software.getSoftwareUrl(),"",softwareName,"0");
    }
    public void insert(ProductSoftware productSoftware) {
        mapper.insert(productSoftware);
    }
    public List<ProductSoftware> getAll() {
        return mapper.selectList(null);
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        String softwareName="散装件.zip";
        //ISO8859-1 UTF-8
        System.out.println(new String(softwareName.getBytes("GBK"), "UTF-8"));
    }
}