java 生成bat_java实现生成windows可执行的批处理文件(.bat)

/*** 下载压缩包

*

*@paramid 商户id

*@paramrequest

*@paramresponse

*@returnvoid

*@authorchen.bing

* @Date 2019/11/4 17:35*/@RequestMapping(value= "downloadzip")public voiddownloadzip(String id, HttpServletRequest request, HttpServletResponse response) {if(StringUtils.isBlank(id)) {

LOG.error("商户id为空");return;

}

String rootZipUrl= Global.getProperty("VOUCHER_IMAGE_DIR");

File rootFile= newFile(rootZipUrl);if (!rootFile.exists()) {

LOG.error("当前文件夹不存在", rootZipUrl);return;

}

String zipUrl=rootZipUrl;

SysMerch sysMerch=sysMerchService.selectById(id);if (sysMerch != null) {

String scheme=request.getScheme();

String contextPath=request.getContextPath();

String serverName=request.getServerName();int port =request.getServerPort();

String basePath= scheme + "://" + serverName + ":" + port +contextPath;

String orgCode=sysMerch.getOrgCode();

String merchNo=sysMerch.getMerchNo();

String merchName=sysMerch.getMerchName();

String merchNameDir= rootZipUrl + File.separator +merchName;

String oldZipUrl= rootZipUrl + File.separator + "downloadZip";boolean b =FileUtils.fileNameRenameTo(oldZipUrl,merchNameDir);

zipUrl=oldZipUrl;if(b) {

zipUrl=merchNameDir;

}

String batUrl= zipUrl + File.separator + "安装包" + File.separator + "setup.txt";

File file= newFile(batUrl);if (!file.exists()) {

File parentFile=file.getParentFile();if (!parentFile.exists()) {

parentFile.mkdirs();

}//不存在创建文件和其父目录

FileUtils.createFile(batUrl);

}

setUpMerchLogo(orgCode, merchNo, merchName, request);

String merchLogoBat= "set wind = ws.createshortcut(strDesktop & \"\\%s.lnk\")";

String url= "wind.Arguments=\"%s\"";

String loginUrl= basePath + "/login?org_code_ck_key=" + orgCode + "&merch_no_ck_key=" +merchNo;

String logoBat=String.format(merchLogoBat, merchName);

String merchUrl=String.format(url, loginUrl);

List list = new LinkedList<>();

list.add("@echo off");

list.add("more %0 +5>%temp%yst.vbs");

list.add("%temp%yst.vbs");

list.add("del %temp%yst.vbs");

list.add("exit");

list.add("set ws = createobject(\"wscript.shell\")");

list.add("strDesktop = ws.SpecialFolders(\"Desktop\")");

list.add(logoBat);

list.add("wind.IconLocation = ws.CurrentDirectory & \"\\favicon.ico\"");

list.add("wind.targetpath = \"%ProgramFiles%\\Internet Explorer\\IEXPLORE.EXE\"");

list.add("wind.workingdirectory = \"%ProgramFiles%\\Internet Explorer\"");

list.add(merchUrl);

list.add("wind.save");

OutputStream outputStream= null;try{//加锁,防止在压缩时有图片写入

synchronized(ZIP_LOCK) {

outputStream= newFileOutputStream(file);//此处循环操作是因为要将.bat文件转换为windows格式

for (int i = 0; i < list.size(); i++) {byte[] bytes = list.get(i).getBytes(Charset.forName("GB2312"));

outputStream.write(bytes);if (i

outputStream.write("\r\n".getBytes(Charset.forName("GB2312")));

}

}

outputStream.close();

String batPath= zipUrl + File.separator + "安装包" + File.separator + "setup.bat";

File batFile= newFile(batPath);

batFile.delete();

FileUtils.fileNameRenameTo(batUrl,batPath);

FileUtils.downloadZip(response, rootZipUrl, merchName);

FileUtils.fileNameRenameTo(merchNameDir, oldZipUrl);file.delete();

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值