测试 用electron + springboot 构建桌面应用
whyczh
2022-03-18 a3f31ff8adf23c1db33b7806f57dd5a23ebaa192
更新
1个文件已添加
3个文件已修改
225 ■■■■ 已修改文件
build.gradle 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/testElectronJ/FboDataInf.java 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/testElectronJ/TestController.java 162 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/testElectronJ/util/ServletUtils.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
build.gradle
@@ -28,6 +28,12 @@
        (unixStartScriptGenerator as TemplateBasedScriptGenerator).template = resources.text.fromFile("customUnixStartScript.txt")
        (windowsStartScriptGenerator as TemplateBasedScriptGenerator).template = resources.text.fromFile("customWindowsStartScript.txt")
    }
    dependencies {
        compile group: 'org.apache.poi', name: 'poi', version: '3.9';
        compile group: 'org.apache.poi', name: 'poi-excelant', version: '3.9';
        compile group: 'org.apache.poi', name: 'poi-scratchpad', version: '3.9';
    }
}
task("setupNode") {
src/main/java/testElectronJ/FboDataInf.java
@@ -73,49 +73,6 @@
    }
    public void readFboFileStream(FileInputStream fis)
    {
        try {
            byte[] buf = new byte[256];
            if(fis.read(buf, 0, buf.length) == 256)
            {
                this.fboDataStart.setDataInf(buf,this.fboDataStop);
                FboData.DataType mType = new FboData().new DataType();
                while(true)
                {
                    int tag = mType.checkDataHead(fis);
                    if((0xFD == tag) || (0xFC == tag) || (0xFB == tag))
                    {
                        byte[] databuf = new byte[this.fboDataStart.BattGroup*14 + 40];
                        if(fis.read(databuf) == databuf.length)
                        {
                            FboData m_FboData = new FboData(this.fboDataStart.BattGroup);
                            m_FboData.m_DataType = mType;
                            m_FboData.setData(databuf);
                            fboData.add(m_FboData);
                        }
                    }
                    if(tag == 1)
                        break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != fis)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        FboDataInf fbo = new FboDataInf();
        //fbo.readFboFile("D:/test/F2022-03-09 11.26.12.FBX");
src/main/java/testElectronJ/TestController.java
@@ -1,10 +1,21 @@
package testElectronJ;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import testElectronJ.util.ServletUtils;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("test")
@@ -25,12 +36,149 @@
        return new Response().set(1,fboDataInf,filePath);
    }
    @PostMapping("/readFboFileStream")
    public Response readFboFileStream(@RequestParam FileInputStream fis) {
    @PostMapping("/export")
    public void export(HttpServletRequest req, HttpServletResponse resp){
        String lbottom_echart = req.getParameter("lbottom_echart");        //电池电流折线图
        String rbottom_echart = req.getParameter("rbottom_echart");        //单体电压折现图
        String rl_echart = req.getParameter("ltop_echart");            //单体容量柱状图
        String sjbg_echart = req.getParameter("rtop_echart");            //数据表格
        String zddy_echart = req.getParameter("ltop_echart");            //组端电压
        String dtdy_echart = req.getParameter("rtop_echart");            //单体电压折线图
        //int battGroupId =Integer.valueOf(req.getParameter("battGroupId"));  //电池组编号
        //int testRecordCount =Integer.valueOf(req.getParameter("testRecordCount"));  //测试编号
        String filePath = req.getParameter("filePath");
        FboDataInf fboDataInf = new FboDataInf();
        //String filePath1 = java.net.URLDecoder.decode(filePath,"utf-8");
        fboDataInf.readFboFileStream(fis);
        return new Response().set(1,fboDataInf);
        fboDataInf.readFboFile(filePath);
        List<FboData> list = fboDataInf.fboData;
        List<byte[]> bytes = new ArrayList<>();
        try {
            if (ServletUtils.isNotNull(lbottom_echart)) {
                String[] url = lbottom_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(rbottom_echart)) {
                String[] url = rbottom_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(rl_echart)) {
                String[] url = rl_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(sjbg_echart)) {
                String[] url = sjbg_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(zddy_echart)) {
                String[] url = zddy_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }if (ServletUtils.isNotNull(dtdy_echart)) {
                String[] url = dtdy_echart.split(",");
                bytes.add(new BASE64Decoder().decodeBuffer(url[1]));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //当前日期
        String nowFormat = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String excelName = "FGCD-"+nowFormat;
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        //图片元素
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        //字体格式-加粗
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        //新建行
        //抬头
        int rowNum = 0;
        HSSFRow rowTitle = sheet.createRow(rowNum);
        HSSFCell cellTitle = rowTitle.createCell(0);
        cellTitle.setCellValue("一体机充放电数据");
        cellTitle.setCellStyle(cellStyle);
        rowNum++;
        //属性栏
        HSSFRow row = sheet.createRow(rowNum);
        row.createCell(0).setCellValue("测试时长"); //创建单元格
        row.createCell(1).setCellValue("总电压");
        row.createCell(2).setCellValue("测试容量");
        row.createCell(3).setCellValue("剩余容量");
        for(int i=0;i<5;i++){
            row.createCell(4+i).setCellValue("#"+Integer.valueOf(i+1));
        }
        rowNum++;
        //数据栏
        if(5!=0){
            for (int i = 0; i < list.size(); i++) {
                sheet.createRow(rowNum);  //创建行
                for(int j=0;j<5;j++){
                    FboData fbo=list.get(i+j);
                    sheet.getRow(rowNum).createCell(0).setCellValue(fbo.m_TestTime.toString());
                    sheet.getRow(rowNum).createCell(1).setCellValue(fbo.SumVoltage);
                    sheet.getRow(rowNum).createCell(2).setCellValue(fbo.testCap);
                    sheet.getRow(rowNum).createCell(3).setCellValue(fbo.residualCap);
                    //sheet.getRow(rowNum).createCell(4+j).setCellValue(fbo.getMonVol());
                }
                i=i+5-1;
                rowNum++;
            }
        }
        rowNum+=2;
        //插入图片
        int picNum = 0;
        String[] picName = new String[]{
                "电池电流折线图","单体电压柱状图","单体容量柱状图","数据表格","组端电压","单体电压折线图"
        };
        //for(int i=0;i<picName.length;i++){
        //    sheet.createRow(rowNum-1);
        //    sheet.getRow(rowNum-1).createCell(0).setCellValue(picName[i]);
        //    //rowNum++;
        //    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,(short) 0, rowNum, (short) 10, rowNum+27);
        //    anchor.setAnchorType(3);
        //    patriarch.createPicture(anchor,wb.addPicture(bytes.get(picNum),HSSFWorkbook.PICTURE_TYPE_PNG)).resize(1);
        //    picNum ++;rowNum+=27;
        //}
        rowNum++;
        try {
            // 转码防止乱码
            resp.addHeader("Content-Disposition", "attachment;filename="
                    + new String(excelName.getBytes("UTF-8"), "ISO8859-1")
                    + ".xls");
            OutputStream out = resp.getOutputStream();
            wb.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/readFboFileStream")
    public Response readFboFileStream(@RequestParam("mFile") MultipartFile mFile, HttpServletRequest request) throws IOException {
        if (!mFile.isEmpty()){
            File file = new File(mFile.getOriginalFilename());
            System.out.println(file.getPath());
            FboDataInf fboDataInf = new FboDataInf();
            fboDataInf.readFboFile(file);
            return new Response().set(1,fboDataInf);
        }else {
            return new Response().set(0,"文件为空");
        }
    }
}
src/main/java/testElectronJ/util/ServletUtils.java
New file
@@ -0,0 +1,14 @@
package testElectronJ.util;
public class ServletUtils {
    public static boolean isNotNull(Object obj){
        boolean flag=false;
        if(obj!=null){
            if(obj.toString().trim().length()>0){
                flag=true;
            }
        }
        return flag;
    }
}