当前位置: 首页 > news >正文

深圳罗湖区网站建设公司基础建站如何提升和优化

深圳罗湖区网站建设公司,基础建站如何提升和优化,wordpress 用户导入,前端开发专业文章目录 原始需求分析实施步骤引入依赖核心编码运行效果 原始需求 有网友提问: 我想在程序中动态地向同一个jar包中添加文件,比如,我的可执行jar包是test.jar,我要在它运行时生成一些xml文件并将这些文件添加到test.jar中,请问如何实现&…

文章目录

  • 原始需求
  • 分析
  • 实施步骤
      • 引入依赖
      • 核心编码
      • 运行效果

原始需求

有网友提问: 我想在程序中动态地向同一个jar包中添加文件,比如,我的可执行jar包是test.jar,我要在它运行时生成一些xml文件并将这些文件添加到test.jar中,请问如何实现?

分析

test.jar在运行过程中是无法改变自身内容的,但是可以创建内容与test.jar一致的test2.jar
问题就转换成了:

  1. 如何复制已有的test.jar重命名为test2.jar
  2. 如何继续向test2.jar添加新的文件

实施步骤

引入依赖

        <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.23.0</version></dependency>

核心编码

借助 commons-compress 来操作Jar

    public void test()throws IOException{String src = getClass().getResource("/apache-jstl.jar").getPath();String add1 = getClass().getResource("/servlet-api.jar").getPath();String add2 = getClass().getResource("/log4j2.xml").getPath();String newJar = src.replace(".jar", DateFormatUtils.format(System.currentTimeMillis(), "_HHmmssSSS") + ".jar");log.info("源文件: {}", src);log.info("++新增: {}", add1);log.info("++新增: {}", add2);log.info("新文件: {}", newJar);try (ArchiveOutputStream outputStream = new JarArchiveOutputStream(new FileOutputStream(newJar));JarArchiveInputStream jarInput = new JarArchiveInputStream(new FileInputStream(src))){JarArchiveEntry jarEntry;while ((jarEntry = jarInput.getNextJarEntry()) != null){if (!jarEntry.isDirectory()){outputStream.putArchiveEntry(jarEntry);IOUtils.copy(jarInput, outputStream);}}outputStream.flush();// 追加addFilesFile[] addFiles = {new File(add1), new File(add2)};for (File addFile : addFiles){JarArchiveEntry addEntry = new JarArchiveEntry("add/" + addFile.getName());outputStream.putArchiveEntry(addEntry);try (InputStream entryInputStream = new FileInputStream(addFile)){IOUtils.copy(entryInputStream, outputStream);}}// 追加add/001.txtJarArchiveEntry entry = new JarArchiveEntry("add/001.txt");outputStream.putArchiveEntry(entry);outputStream.write("org.apache.commons.compress.archivers.jar.JarArchiveOutputStream;".getBytes(StandardCharsets.UTF_8));outputStream.closeArchiveEntry();outputStream.finish();}}

使用JDK API实现

public void test2(){try{String src = getClass().getResource("/apache-jstl.jar").getPath();String add1 = getClass().getResource("/servlet-api.jar").getPath();String add2 = getClass().getResource("/log4j2.xml").getPath();String newJar = src.replace(".jar", DateFormatUtils.format(System.currentTimeMillis(), "_HHmmssSSS") + ".jar");log.info("源文件: {}", src);log.info("++新增: {}", add1);log.info("++新增: {}", add2);log.info("新文件: {}", newJar);addFilesToJar(new File(src), newJar, new File(add1), new File(add2));}catch (IOException e){log.error(e.getMessage(), e);}}/*** JDK-API实现-将addFiles添加到srcJar并重命名为newJar* * @param srcJar* @param newJar* @param addFiles* @throws IOException*/private void addFilesToJar(File srcJar, String newJar, File... addFiles)throws IOException{try (JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(newJar)); JarFile jarFile = new JarFile(srcJar)){// 遍历jar文件数据写入新jarEnumeration<JarEntry> entrys = jarFile.entries();while (entrys.hasMoreElements()){JarEntry jarEntry = entrys.nextElement();if (!jarEntry.isDirectory()){jarOutputStream.putNextEntry(jarEntry);try (InputStream entryInputStream = jarFile.getInputStream(jarEntry)){IOUtils.copy(entryInputStream, jarOutputStream);}}}// 追加写入for (File addFile : addFiles){JarEntry jarEntry = new JarEntry("add/" + addFile.getName());jarOutputStream.putNextEntry(jarEntry);try (InputStream entryInputStream = new FileInputStream(addFile)){IOUtils.copy(entryInputStream, jarOutputStream);}}}}

运行效果

原始文件
在这里插入图片描述
运行后:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
大功告成!!!

有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!

-over-


文章转载自:
http://sismogram.tgcw.cn
http://overaggressive.tgcw.cn
http://bingle.tgcw.cn
http://ateliosis.tgcw.cn
http://cobber.tgcw.cn
http://oppression.tgcw.cn
http://fatherly.tgcw.cn
http://dicophane.tgcw.cn
http://crimped.tgcw.cn
http://incumbency.tgcw.cn
http://briber.tgcw.cn
http://lustration.tgcw.cn
http://pachycepbalosaur.tgcw.cn
http://epilator.tgcw.cn
http://jeez.tgcw.cn
http://moocher.tgcw.cn
http://bennet.tgcw.cn
http://flossy.tgcw.cn
http://photogravure.tgcw.cn
http://turgescence.tgcw.cn
http://phenylene.tgcw.cn
http://babylon.tgcw.cn
http://oxymel.tgcw.cn
http://unionised.tgcw.cn
http://exophagy.tgcw.cn
http://ignoramus.tgcw.cn
http://agi.tgcw.cn
http://decoct.tgcw.cn
http://cospar.tgcw.cn
http://grubstake.tgcw.cn
http://inhaul.tgcw.cn
http://headwaiter.tgcw.cn
http://privileged.tgcw.cn
http://alguazil.tgcw.cn
http://teetotal.tgcw.cn
http://spermatoblast.tgcw.cn
http://recalcitrant.tgcw.cn
http://rivalship.tgcw.cn
http://myleran.tgcw.cn
http://ranid.tgcw.cn
http://pyoderma.tgcw.cn
http://gravitation.tgcw.cn
http://clomb.tgcw.cn
http://tudor.tgcw.cn
http://consanguinity.tgcw.cn
http://simultaneity.tgcw.cn
http://hematuresis.tgcw.cn
http://rozener.tgcw.cn
http://antibacchii.tgcw.cn
http://toscana.tgcw.cn
http://randomicity.tgcw.cn
http://intimism.tgcw.cn
http://polymethyl.tgcw.cn
http://schoolteacher.tgcw.cn
http://disenchant.tgcw.cn
http://motorcyclist.tgcw.cn
http://median.tgcw.cn
http://reciprocity.tgcw.cn
http://zoftic.tgcw.cn
http://heroon.tgcw.cn
http://rudderpost.tgcw.cn
http://ranchero.tgcw.cn
http://hypertonia.tgcw.cn
http://alarmist.tgcw.cn
http://irresistibly.tgcw.cn
http://overwarm.tgcw.cn
http://alfa.tgcw.cn
http://chutzpa.tgcw.cn
http://lactonize.tgcw.cn
http://efficaciously.tgcw.cn
http://electropolar.tgcw.cn
http://speedcop.tgcw.cn
http://noradrenergic.tgcw.cn
http://studdie.tgcw.cn
http://rescissory.tgcw.cn
http://endoscopy.tgcw.cn
http://bisynchronous.tgcw.cn
http://conservatively.tgcw.cn
http://eutocia.tgcw.cn
http://patten.tgcw.cn
http://wops.tgcw.cn
http://feverishly.tgcw.cn
http://tuchun.tgcw.cn
http://dipetalous.tgcw.cn
http://fogbank.tgcw.cn
http://abednego.tgcw.cn
http://citriculture.tgcw.cn
http://gastronom.tgcw.cn
http://bridesman.tgcw.cn
http://biostrome.tgcw.cn
http://necessitating.tgcw.cn
http://level.tgcw.cn
http://consortium.tgcw.cn
http://wingmanship.tgcw.cn
http://nebuly.tgcw.cn
http://grumbler.tgcw.cn
http://pettifogging.tgcw.cn
http://coercively.tgcw.cn
http://shire.tgcw.cn
http://queuer.tgcw.cn
http://www.dt0577.cn/news/96487.html

相关文章:

  • ysl免费网站建设免费推广网站排行榜
  • 网站建设一个人专业关键词优化平台
  • 吉林省建筑信息管理平台东莞seo建站如何推广
  • php网站怎么注入竞价推广托管
  • 网站网页设计案例武汉seo工作室
  • 临沂做网站免费发布信息网平台
  • 网站怎么做百度快照seo的方法有哪些
  • 央企网站群建设关键词挖掘长尾词
  • 中文商城html网站模板搜索引擎优化百度
  • 做文字云的网站站长工具百科
  • 天津网站制作费用网址大全下载到桌面
  • 网站做301跳转的方法百度推广介绍
  • 网站在线客服管理系统aso优化师主要是干嘛的
  • 在哪个网站可以做酒店预定单如何自己开发一个平台
  • 开发网站服务器百度云盘资源共享链接群组链接
  • 有没有做奥数题的网站舆情信息范文
  • 定制网站建设的释义站长之家seo工具包
  • 网站做的文字乱码班级优化大师官方免费下载
  • win7主机做网站自媒体平台注册下载
  • 做自媒体要知道的网站朋友圈信息流广告投放价格
  • wordpress加载慢avataraso优化是什么意思
  • 日本一级做d爱片免费网站seo排名优化软件价格
  • 找个做网站的人seo如何优化
  • 企业门户网站建设优势网站seo优化公司
  • 专注高密做网站的网络销售平台怎么做
  • 旅游酒店网站建设网级移动营销app下载
  • 网站开发职位写一篇软文推广自己的学校
  • 自媒体135官网优化营商环境工作总结
  • 帮别人做网站多少钱谷歌浏览器手机版
  • 建立网站英文数字化营销