whyczh
2022-05-09 9cdd5ffcc0f7e6844913aaa2c29391cc16c78ecd
电池组新增运行build.exe
1个文件已修改
59 ■■■■■ 已修改文件
src/main/java/com/whyc/service/BattInfService.java 59 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/BattInfService.java
@@ -13,6 +13,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
@@ -146,7 +149,62 @@
        battInfMapper.insert(battinf);
        battInfMapper.insertUserBattgroupBaojigroupBattgroup(battinf.getStationId(),battinf.getBattGroupId());
        battInfMapper.insertUserBattgroupBaojigroupBattgroupSelect(battinf.getStationId(),battinf.getBattGroupId(),userId);
        run_cmd();
        return new Response().set(1,battinf,"添加成功");
    }
    //启动创建数据库的jar文件
    public static void run_cmd() {
        //String path=System.getProperty("user.dir");                //myeclipse中获取的目录(获取到的是tomcat/bin)和分离项目获取到的路径(tomcat)不一样
        String fullRealPath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String path = fullRealPath.split(File.separator+"webapps")[0];
        System.out.println("tomcat的路径为"+path);
        //识别服务器的系统类型
        String OS = System.getProperty("os.name").toLowerCase();
//        System.out.println(OS);
        String strcmd="";
        if(OS.contains("window")) {
            String realapth=path.substring(0,path.lastIndexOf("\\"));
            strcmd="cmd /c start  "+realapth+"/webapps/cmd_BTSE_DB_Builder.cmd";
            String batPath = realapth+"/webapps/cmd_BTSE_DB_Builder.cmd";
            File f = new File(path);
            //System.out.println("File :"+f.getParentFile().getAbsolutePath()+File.separator+"Batt_MS_FBSDEV_X64/Batt_MS_FBSDEV/"+"cmd_BTSE_DB_Builder.cmd");
            //batPath = f.getParentFile().getAbsolutePath()+File.separator+"Batt_MS_FBSDEV_X64/Batt_MS_FBSDEV/"+"cmd_BTSE_DB_Builder.cmd";
            String jarPath = "";
            if(path.endsWith("bin")){
                f = f.getParentFile();
            }
            jarPath = f.getParentFile().getAbsolutePath()+File.separator+"Batt_MS_FBSDEV_X64/Batt_MS_FBSDEV/"+"BTSE_DB_Builder.exe";                //主程序中jar的目录
            System.out.println("jar执行文件全路径为:"+jarPath);
            //strcmd = "cmd /c start "+batPath;
            strcmd = ("cmd /c start ")+jarPath.replaceAll(" ", "\" \"");
        }else {
            strcmd="java -jar /app/BTSE_DB_Builder.jar";
        }
        //System.out.println(path);
        //修复绝对路径中存在空格时执行不成功bug
        Process child = null;
        InputStream in = null;
        try {
            //System.out.println(strcmd);
            child = Runtime.getRuntime().exec(strcmd);
            in = child.getInputStream();
            int c;
            while ((c = in.read()) != -1) {
                //System.out.print((char)c);
            }
            in.close();
            try {
                child.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //System.out.println("Run Bat OK....");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public Response searchInfo(int pageNum,int pageSize,Integer userId,String stationName1,String stationName2,String stationName5,String stationName3){
@@ -163,6 +221,7 @@
    public Response update(Battinf battinf){
        if (battInfMapper.updateById(battinf)>0){
            run_cmd();
            return new Response().set(1,"更新成功");
        }else {
            return new Response().set(0,"更新失败");