itextpdf设置页码_word页码相同怎么改

itextpdf设置页码_word页码相同怎么改项目需要样式比较复杂,刚开始因为时间比较紧所以采用了Itextpdf插件代码生成pdf,实话说过程十分繁琐,因为pdf文件样式比较多,表格也比较多,各种的表格,还有就是页眉页脚页码都要自己找页面位置坐标但是又不能像html那样方便更改查看样式,只能改一点导出来看看合适不,然后再改再导出来看。私下调研了很多,现在罗列一下我所用到的一些样式处理,希望可以帮助需要做这个功能的朋友少踩坑,少尝试,直接出完美pdf报告。封面首页封面右上角那个图片可以换成logo,我做的大概是这样的一个样子,上代码/

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

项目需要样式比较复杂,刚开始因为时间比较紧所以采用了Itextpdf插件代码生成pdf ,实话说过程十分繁琐,因为pdf文件样式比较多,表格也比较多,各种的表格,还有就是页眉页脚页码都要自己找页面位置坐标但是又不能像html那样方便更改查看样式,只能改一点导出来看看合适不,然后再改再导出来看。
私下调研了很多,现在罗列一下我所用到的一些样式处理,希望可以帮助需要做这个功能的朋友少踩坑,少尝试,直接出完美pdf报告。

  1. 封面
    在这里插入图片描述
  2. 首页在这里插入图片描述
    封面右上角那个图片可以换成logo,我做的大概是这样的一个样子,上代码
//定义 页面大小,以及页边距左右上下
package com.example.demo.controller;

import com.example.demo.config.PDFConfig;
import com.example.demo.utils.PDFBuilder;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author hongli.zhang
 * @create 2020/5/26 13:02
 **/

@RestController
@RequestMapping("/businessApplication/")
public class PdfContrller {

    @Autowired
    PDFConfig config;//这个是配置类,里面设置了需要用到的字体和logo图片的绝对路径
    public static void setImg(PdfWriter writer, String path, float fitWidth, float fitHeight, float absoluteX, float absoluteY) {
        try {
            Image image = Image.getInstance(path);
            image.setAlignment(Image.MIDDLE);
            image.scaleToFit(fitWidth, fitHeight);
            image.setAbsolutePosition(absoluteX, absoluteY);
            writer.getDirectContent().addImage(image);
        }catch(Exception e){
        }
    }
    @GetMapping("/test")
    public void test(HttpServletRequest requ, HttpServletResponse resp) throws DocumentException {

//设置页面大小为A4纸大小,以及页边距左右上下
        Document document = new Document(PageSize.A4, 100F, 100F, 80F, 120F);
        PdfWriter writer = null;
        try {
            FileOutputStream out = new FileOutputStream("E:\\new.pdf");
            writer = PdfWriter.getInstance(document, out);
        } catch ( IOException | DocumentException e) {
            e.printStackTrace();
        }
        // 定义页眉和页脚页码事件,PDFBuilder代码在后面
        PDFBuilder builder = new PDFBuilder();
        //设置页面监听事件
        writer.setPageEvent(builder);

        document.open();
        //换行
        Paragraph newLine = new Paragraph("\n");
        float tableWidthPercentage = 100f; //表格的整体宽度

        //表格背景色
        BaseColor green = new BaseColor(175, 215, 136);
        BaseColor blue = new BaseColor(148, 170, 214);

        //图标
        setImg(writer, config.getLogo(), 130, 100, document.getPageSize().getRight(230), 700);
        //所需字体
        String fontPath = config.getCalibri();
        String fontBlodPath = config.getCalibrib();
        BaseFont bf = null;
        BaseFont blodbf = null;
//        BaseFont chineseFont = null;
//        BaseFont chineseBlodFont = null;
        try {
            bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            blodbf = BaseFont.createFont(fontBlodPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//            chineseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
        Font coverFont = new Font(blodbf, 30, Font.NORMAL);
        Font titleFont = new Font(blodbf, 16, Font.NORMAL);
        Font coverTiletFontMarked = new Font(blodbf, 16f, Font.NORMAL, new BaseColor(148, 170, 214));
        Font textFontBold = new Font(blodbf, 10.5f, Font.NORMAL);
        Font textFont = new Font(bf, 10.5f, Font.NORMAL);
        Font textFontGray = new Font(blodbf, 10.5f, Font.NORMAL, new BaseColor(215, 215, 215));
        Font chapterFont = new Font(blodbf, 14f, Font.NORMAL);
//        Font coverFont = new Font(chineseFont, 30, Font.NORMAL);
//        Font titleFont = new Font(chineseFont, 16, Font.NORMAL);
//        Font coverTiletFontMarked = new Font(chineseFont, 16f, Font.NORMAL, new BaseColor(148, 170, 214));
//        Font textFontBold = new Font(chineseFont, 10.5f, Font.NORMAL);
//        Font textFont = new Font(chineseFont, 10.5f, Font.NORMAL);
//        Font textFontGray = new Font(chineseFont, 10.5f, Font.NORMAL, new BaseColor(215, 215, 215));
//        Font chapterFont = new Font(chineseFont, 14f, Font.NORMAL);


        //封面
        Paragraph cover = new Paragraph("\n\n\n"+"CompanyName In Here HAHAHAHAH"+"\nMy Company REPORT",coverFont);
        cover.setAlignment(Element.ALIGN_CENTER);
        Paragraph paragraph1 = new Paragraph("\n\n\nREPORT DATE: " +"2020/05/03 12:02:15", titleFont);
        paragraph1.setAlignment(Element.ALIGN_RIGHT);
        cover.add(paragraph1);
        cover.add(new LineSeparator(5f, 100, new BaseColor(148, 170, 214), Element.ALIGN_CENTER, 30f));
        cover.add(newLine);
        Paragraph paragraph2 = new Paragraph("REPORT ID: " + "B124568744", coverTiletFontMarked);
        paragraph2.setAlignment(Element.ALIGN_RIGHT);
        cover.add(paragraph2);
        document.add(cover);


        document.newPage();
        //添加首页
        Paragraph home = new Paragraph(new Chunk("MY Report\n", chapterFont));
        home.add(new Chunk("Report Type: Entity\n\n", textFontGray));
        home.add(new Chunk("Company Name:\n", textFontBold));
        home.add(new Chunk("JKLJI HJKHK NJNJ UHIU", titleFont));
        document.add(home);
//        document.add(line);

        document.add(newLine);
        document.add(newLine);
        PdfPTable riskDashTb = new PdfPTable(2);
        riskDashTb.setWidths(new float[]{0.4f, 0.6f});// 每个单元格占多宽
        riskDashTb.setWidthPercentage(tableWidthPercentage);
        riskDashTb.addCell(getCell(new Phrase("level", textFontBold), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("countryCode", textFont), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("Amount", textFontBold), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("nationality", textFont), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("Converted Credit Amount", textFontBold), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("addr", textFont), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("currency", textFontBold), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("busiZip", textFont), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("level", textFontBold), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("countryCode", textFont), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("Amount", textFontBold), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("nationality", textFont), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("Converted Credit Amount", textFontBold), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("addr", textFont), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("currency", textFontBold), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("busiZip", textFont), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("level", textFontBold), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("countryCode", textFont), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("Amount", textFontBold), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("nationality", textFont), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("Converted Credit Amount", textFontBold), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("addr", textFont), blue, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("currency", textFontBold), null, 1, 1));
        riskDashTb.addCell(getCell(new Phrase("busiZip", textFont), null, 1, 1));
        document.add(riskDashTb);
        document.add(newLine);

        document.add(new Paragraph("Note: The sdsd sdsd dsds is sd through a sdsds model that sdsd the sdsd of dsd of the sdsdsd company. aaa Amount is asas asa on sds saa dsdand other sdsd information.", textFont));
        document.add(newLine);
        Paragraph end = new Paragraph("End of the My Report", titleFont);
        end.setAlignment(Element.ALIGN_CENTER);
        document.add(end);
        document.close();

//            resp.getWriter().print("success");
    }


    private PdfPCell getCell(Phrase phrase, BaseColor color, int colSpan, int rowSpan) {
        PdfPCell cells = new PdfPCell(phrase);
        cells.setUseAscender(true);
        cells.setMinimumHeight(20f);
        cells.setHorizontalAlignment(Element.ALIGN_LEFT);
        cells.setVerticalAlignment(5);
        cells.setColspan(colSpan);
        cells.setRowspan(rowSpan);
        cells.setNoWrap(false);
        if (color != null) {
            cells.setBackgroundColor(color);
        }
        return cells;
    }


}

接下来是页眉页脚事件的代码PDFBUilder
因为页眉页脚都是带格式的,所以需要一点一点拼接起来,页眉中需要添加logo图片,位置是通过代码中的x,y坐标指定的

package com.example.demo.utils;

import com.example.demo.config.PDFConfig;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 设置页面附加属性
 *
 * @author hongli.zhang
 * @create 2020/5/18 21:00
 **/
@Component
public class PDFBuilder extends PdfPageEventHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(PDFBuilder.class);

    public Phrase header;

    public Phrase footer;

    // 模板
    public PdfTemplate total;

    // 基础字体对象
    public BaseFont bf = null;

    // 利用基础字体生成的字体对象,一般用于生成中文文字
    public Font fontDetail = null;

    public PDFBuilder() {

    }

    public PDFBuilder(Phrase header, Phrase footer) {
        this.header = header;
        this.footer = footer;
    }

    public void setHeader(Phrase header) {
        this.header = header;
    }

    public void setFooter(Phrase footer) {
        this.footer = footer;
    }

    /**
     * 文档打开时创建模板
     *
     * @param writer
     * @param document
     */
    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(50, 50);// 共 页 的矩形的长宽高
    }

    /**
     * 关闭每页的时候,写入页眉,页脚。
     *
     * @param writer
     * @param document
     */
    public void onEndPage(PdfWriter writer, Document document) {
        this.addPage(writer, document);
        //this.addWatermark(writer);
    }

    public static void setImg(PdfWriter writer, String path, float fitWidth, float fitHeight, float absoluteX, float absoluteY) {
        try {
            Image image = Image.getInstance(path);
            image.setAlignment(Image.MIDDLE);
            image.scaleToFit(fitWidth, fitHeight);
            image.setAbsolutePosition(absoluteX, absoluteY);
            writer.getDirectContent().addImage(image);
        } catch (Exception e) {
            LOGGER.error("[ERROR] Set Img : img file does not exist.");
        }
    }

    //加分页
    public void addPage(PdfWriter writer, Document document) {
        PDFConfig config = InjectUtil.getInstance().getConfig();
        if (document.getPageNumber() > 1) {
            try {
                bf = BaseFont.createFont(config.getCalibri(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            BaseFont blodf = null;
            try {
                blodf = BaseFont.createFont(config.getCalibrib(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            fontDetail = new Font(bf, 9, Font.NORMAL);// 数据体字体
            Font titleFnot = new Font(blodf, 10.5f, Font.NORMAL);
            Font blodFnot = new Font(blodf, 9f, Font.NORMAL);
            Font font = new Font(bf, 10.5f, Font.NORMAL);

            setImg(writer, "D:\\ceshi.jpg", 85, 45, 65, 785);
            Phrase pageNumberPh = new Phrase(String.valueOf(document.getPageNumber() - 1), fontDetail);
            float center = document.getPageSize().getRight() / 2;//页面的水平中点
            float left = document.getPageSize().getLeft(90);//页面的z左边距
            float headerleft = document.getPageSize().getLeft(180);//页面的z左边距
            float right = document.getPageSize().getRight(90);//页面的z左边距
            float top = document.getPageSize().getTop() - 36;
            float bottom = document.getPageSize().getBottom() + 36;

            header = new Paragraph("Beijing XXXX Information Technology Hn.,JKI.", titleFnot);
            Paragraph header1 = new Paragraph("5f, building C2, hkhkjhd Industrial Park,", font);
            Paragraph header2 = new Paragraph("No. 44, Changhu Road, Jiangxi Industrial Park", font);
            Paragraph header3 = new Paragraph("www.xxx.com", font);
            Paragraph header4 = new Paragraph("China (sss) xxxxxxxxxxxx Dgyhi Code Zone", font);
            Paragraph header5 = new Paragraph("400-1234-567", font);
            Paragraph footer1 = new Paragraph("Acnnecfds Kdjklsnf", blodFnot);
            Paragraph footer2 = new Paragraph("The dfdfdf fddffdfdf by fdfdf in thfdfiscxcx fdfd is based on fdfd fdfd has been fdf fdfdf fdfdfdfd erwerwerwerwrww",
                    new Font(bf, 9, Font.NORMAL));
            Paragraph footer3 = new Paragraph("ffdfdf and fdfdf fffdfd. fdsf fdfd fdfdf xcxcfdfd fdf fdf fdfd to fdfd that the dfdfdssxx is fdfdfdf and werwerwe",
                    new Font(bf, 9, Font.NORMAL));
            Paragraph footer4 = new Paragraph("seewwdrr, we fdf fdfdf fdfd any fdfdfdxcx for fddfdfdfd, fdfdf, fdferere rererer or rer-rere rer and a werwerwer",
                    new Font(bf, 9, Font.NORMAL));
            Paragraph footer5 = new Paragraph("rerer or rer of the rererer in rere rerer cxcxcxcxrer rere the retg of tfhe fffgvfgddsss on its sdfsd. The dddddw ",
                    new Font(bf, 9, Font.NORMAL));
            Paragraph footer6 = new Paragraph("information dsfsdf is DSFSDS DFFDDD and Sdcxvxcvdfdfsd not be dfsdfsdf to any sdfsdf sdfsdfsd the dsfsdfsfdsdfee ",
                    new Font(bf, 9, Font.NORMAL));
            Paragraph footer7 = new Paragraph("erdsfsdfsd. sdfsdf dsfsdf & dsfsdf dsfsdf sdfsdf of any sfddsfsdf dfdfdfdf the dfdf of the inforfmationssdeeetyy ",
                    new Font(bf, 9, Font.NORMAL));
//            Paragraph footer8 = new Paragraph("dfdfddff in this report.", new Font(bf, 9, Font.NORMAL));
            Paragraph footer9 = new Paragraph("[fdddfdfdfdf]", blodFnot);

//                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, headerImg, center, top, 0); //页眉图标
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header, headerleft, top + 13, 0); //页眉标题
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header1, headerleft, top - 3, 0); //页眉内容
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header2, headerleft, top - 15, 0); //页眉
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header3, right, top - 15, 0); //页眉
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header4, headerleft, top - 30, 0); //页眉
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header5, right, top - 30, 0); //页眉
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, pageNumberPh, right, bottom + 65, 0); //页码
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footer1, center, bottom + 60, 0); //页脚标题
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer2, left, bottom + 50, 0); //页脚
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer3, left, bottom + 40, 0); //页脚
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer4, left, bottom + 30, 0); //页脚
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer5, left, bottom + 20, 0); //页脚
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer6, left, bottom + 10, 0); //页脚
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer7, left, bottom, 0); //页脚
//            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, footer8, left, bottom - 10, 0); //页脚
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footer9, center, bottom - 20, 0); //页脚
        }
    }

    //加水印
    public void addWatermark(PdfWriter writer) {
        // 水印图片
        Image image;
        try {
            image = Image.getInstance("");
            PdfContentByte content = writer.getDirectContentUnder();
            content.beginText();
            // 开始写入水印
            for (int k = 0; k < 5; k++) {
                for (int j = 0; j < 4; j++) {
                    image.setAbsolutePosition(150 * j, 170 * k);
                    content.addImage(image);
                }
            }
            content.endText();
        } catch (IOException | DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     *
     *
     * TODO 关闭文档时,替换模板,完成整个页眉页脚组件
     * @see PdfPageEventHelper#onCloseDocument(PdfWriter,
     *      Document)
     */


    public void onCloseDocument(PdfWriter writer, Document document) {
        // 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。
        total.beginText();
        total.setFontAndSize(bf, 9);// 生成的模版的字体、颜色
        String foot2 = " " + (writer.getPageNumber()) + " 页"; //页脚内容拼接  如  第1页/共2页
        total.showText(foot2);// 模版显示的内容
        total.endText();
        total.closePath();
    }
}

大概覆盖了大部分的功能,当然还有给字体加背景色,字体加颜色,和比较复杂的表格制作
比如需要将list中的数据按年份横排
在这里插入图片描述
我罗列出来可能需要用到的网站地址,

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/186444.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • android轮播图实现_ajax异步加载

    android轮播图实现_ajax异步加载这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影。    可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚接触的人来说可能太少了,下面我就把我使用过程中所知道的写了下来,希望可以帮助自己和别人更深入了解这个库的使用和配置。     GITHUB上的下载路径为:https:/

    2022年10月27日
  • centos7配置sftp_怎么修改服务器sftp端口

    centos7配置sftp_怎么修改服务器sftp端口CentOS7修改sftp端口修改sftp默认端口为12322vi/etc/ssh/ssh_config把Port22改成Port12322vi/etc/ssh/sshd_config把Port22改成Port12322修改之后重启servicesshdrestart

  • ipfs是什么项目?filecoin怎么挖矿(filecoin的矿机有哪几家)

    如果考虑成为一个Filecoin矿工,首要需要清楚的是,挖矿收益主要来自三方面:区块打包奖励、数据存储收益、检索收益。1.区块奖励Filecoin网络拥有独特的经济设计,它可以使参与者之间的奖励保持一致,并奖励Filecoin矿工的有效工作。Filecoin经济的一个关键组成部分是区块奖励,这是对存储矿工的补贴,以提供可靠和有用的存储容量并维护Filecoin区块链。大多数区块链使用加密货币令牌奖励生产区块的矿工,以成功挖掘链中的新区块。Filecoin在这方面没有什么不同。Filecoin区块链通过连

  • 一图解密AlphaZero(附Pytorch实践)

    一图解密AlphaZero(附Pytorch实践)知乎专栏同步发布: https://zhuanlan.zhihu.com/p/41133862本来打算自己写写的,但是发现了DavidFoster的神作,看了就懂了。我也就不说啥了。看不清的话,原图在后面的连接也可以找到。没懂?!!!那我再解释下。 AlphaGoZero主要由三个部分组成:自我博弈(self-play),训练和评估。和AlphaGo比较,AlphaZ…

  • AVA测试框架内部的Promise异步流程控制模型

    AVA测试框架内部的Promise异步流程控制模型作者:肖磊个人主页:github最近将内部测试框架的底层库从mocha迁移到了AVA,迁移的原因之一是因为AVA提供了更好的流程控制。我们从一个例子开始入手:有A,B,C,D4个case,我要实现A–>>B–>>(C|D),A最先执行,B等待A执行完再执行,最后是(C|D)并发执行,使用ava提供的API来完成case就是:constava…

    2022年10月31日
  • Tomcat如何打破双亲委派机制实现隔离Web应用的?

    Tomcat通过自定义类加载器WebAppClassLoader打破双亲委托,即重写了JVM的类加载器ClassLoader的findClass方法和loadClass方法,这样做的目的是优先加载Web应用目录下的类。除此之外,你觉得Tomcat的类加载器还需要完成哪些需求呢?或者说在设计上还需要考虑哪些方面?我们知道,Tomcat作为Servlet容器,它负责加载我们的Servlet类,此外它还负责加载Servlet所依赖的JAR包。并且Tomcat本身也是一个Java程序,因此它需要加载自己的类和依赖

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号