java代码打war包

java代码打war包

一、 maven依赖

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
        <!-- 处理压缩解压 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.9</version>
        </dependency>

二、 解压
warPath是war包,unZipPath是生成的目录的名字

 /**
     * 解压war文件
     * @param warPath war文件路径
     * @param unZipPath 解压到的路径
     */
    public static boolean unZipWar(Path warPath, Path unZipPath) {
        String warName = warPath.toFile().getName();
        LOGGER.debug("warName: " + warName);
        LOGGER.debug("unZipPath: " + unZipPath.toString());
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(warPath.toFile()));
             ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(
                     ArchiveStreamFactory.JAR, bis)) {
            if (Files.exists(unZipPath)) {
                Files.delete(unZipPath);
            }
            Files.createDirectories(unZipPath);
            JarArchiveEntry entry;
            while ((entry = (JarArchiveEntry) ais.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    Path currentPath = Paths.get(unZipPath.toString(), entry.getName());
                    if (!Files.exists(currentPath.getParent())) {
                        Files.createDirectories(currentPath);
                    } else if (!Files.exists(currentPath)) {
                        Files.createDirectory(currentPath);
                    }
                } else {
                    Path currentPath = unZipPath.resolve(entry.getName());
                    if (!Files.exists(currentPath.getParent())) {
                        Files.createDirectories(currentPath.getParent());
                    }
                    try (OutputStream os = Files.newOutputStream(currentPath)) {
                        IOUtils.copy(ais, os);
                    }
                }
            }
        } catch (ArchiveException | IOException e) {
            LOGGER.error(ServerMsgConstants.UNPACK_WAR_ERROR + warName, e);
            return false;
        }
        return true;
    }

三、 压缩
对 sourceDir目录进行压缩,后缀是.war,生成sourceDir.war文件

/**
     * 创建 war 文件
     * @param sourceDir
     */
    public static boolean zipWar(Path sourceDir) {
        sourceDir = sourceDir.toAbsolutePath();
        String rootDir = sourceDir.toString();
        LOGGER.debug("rootDir: " + rootDir);
        if (!StringUtils.endsWithFileSeparator(rootDir)) {
            rootDir += File.separator;
        }
        Path warPath = sourceDir.getParent().resolve(StringUtils.appendWarSuffix(sourceDir.getFileName().toString()));
        try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(warPath));
             ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, bos)) {
            Iterator<File> files = FileUtils.iterateFiles(sourceDir.toFile(), null, true);
            LOGGER.debug("rootDir: " + rootDir);
            while (files.hasNext()) {
                File file = files.next();
                ZipArchiveEntry entry = new ZipArchiveEntry(file, StringUtils.remove(file.getAbsolutePath(), rootDir));
                aos.putArchiveEntry(entry);
                try (InputStream is = new FileInputStream(file)) {
                    IOUtils.copy(is, aos);
                }
                aos.closeArchiveEntry();
            }
            aos.flush();
        } catch (IOException | ArchiveException e) {
            LOGGER.error(ServerMsgConstants.PACK_WAR_ERROR, e);
            return false;
        }
        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
无需数据库 本资源含界面预览 本系统没有数据库,您可将目前网络上的任意网站的接口瞬间变成您的数据源,取数据超 easy ------------------------------------------------ -----------------如何部署------------------------- ------------------------------------------------ 下载压缩,将压缩中的 yijavaBlog-1.0.0.war 解压缩到 Tomcat 的 webapps/ROOT 即可访问浏览 ------------------------------------------------ -----------------接口配置的例子------------------- ------------------------------------------------ { name:"mobileSegment", url:"https://api.it120.cc/common/mobile-segment/location", returnType:"json" } 上面配置后,您就可以直接在 html 静态页面上这样写: ${mobileSegment("mobile=13500000000").data.province} 即可在页面上显示 “广东” ,除了 province ,您可以获取接口返回的任意数据哦 ~ ------------------------------------------------ -----------------关于接口------------------------ ------------------------------------------------ 目前有很多网站偶读提供了接口服务,比如 juhe.cn apistore.baidu.com market.aliyun.com/data 您无需担心接口的稳定性和速度等问题 (您自己搭建的数据库能比他们稳定、安全么?) 脱离了数据库还有一个棒棒的原因: 您可以随时更换你的网站服务器,而不要关心数据迁移问题,因为数据本身就是保存在 阿里云、百度、聚合数据 上的 同时,聪明的您或许发现了 : 可以同时部署在多台服务器上,配置一样的接口即可,不就轻松实现分布式集群部署了么 ?~ ------------------------------------------------ -----------------开源项目------------------------ ------------------------------------------------ 本项目是一个开源项目,所以您完全可以免费使用,同时也希望您加入到这个项目的贡献中来,为开源出一份微博之力吧 :~ 开源项目地址: https://github.com/gooking/yijava-blog 有什么好的想法或者建议,欢迎在 github 上面给我留言、互动 ~

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值