FTP图片添加水印,中文乱码问题解决

主要用JAVA 的 awt 给FTP图片添加水印

添加水印核心方法

/**
     * 添加水印核心方法
     * @param bufImg
     * @param img
     * @param text
     * @param font
     * @param color
     * @param x
     * @param y
     */
    private static void mark(BufferedImage bufImg, Image img, List<String> text, Font font, Color color, int x, List<Integer> y) {
        Graphics2D g = bufImg.createGraphics();
        g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
        g.setColor(color);
        g.setFont(font);
        if( text != null && text.size() > 0){
            for (int i = 0; i < text.size(); i++) {
                g.drawString(text.get(i), x, y.get(i));
            }
        }
        g.dispose();
    }

给图片添加文字水印

  /**
     * 给图片增加文字水印
     *
     * @param inputStream
     *            -文件的IO流
     * @param longitude
     *            -经度
     * @param latitude
     *            -纬度
     * @param address
     *            -地址
     * @param currentTime
     *            -时间
     * @param color
     *            -颜色
     */
    private static InputStream mark(InputStream inputStream, String longitude ,String latitude,String address,String currentTime, Color color) {
        try {
            Image img = ImageIO.read(inputStream);
            // 读取原图片信息
            int imgWidth = img.getWidth(null);
            int imgHeight = img.getHeight(null);
            int x = 50;
            // 加水印
            BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
            Font font = new Font("宋体", Font.PLAIN, 40);
            java.util.List<String> textList = Lists.newArrayList();
            textList.add("经度: "+longitude);
            textList.add("纬度: "+latitude);

            textList.add("地址: "+address);
            textList.add("时间: "+currentTime);
            List<Integer> yList = Lists.newArrayList();
            yList.add(imgHeight-180);
            yList.add(imgHeight-130);
            yList.add(imgHeight-80);
            yList.add(imgHeight-30);
            
            mark(bufImg, img, textList, font, color, x, yList);

            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(bufImg, "jpg", imOut);
            InputStream is = new ByteArrayInputStream(bs.toByteArray());

            return is;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

编辑FTP图片

    /**
     * 上传文件
     *
     * @param pathname     路径名称
     * @param fileName     文件名称
     * @param longitude    经度
     * @param latitude     纬度
     * @param address      地址
     * @param currentTime  时间
     * @return
     */
    public static boolean waterMark(String pathname, String fileName, String longitude , String latitude, String address, String currentTime) {
        InputStream inputStream = null;
        InputStream is = null;
        FTPClient ftpClient = null;
        try {
            ftpClient = initFtpClient();
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            CreateDirecroty(pathname,ftpClient);
            ftpClient.makeDirectory(pathname);
            ftpClient.changeWorkingDirectory(pathname);
            ftpClient.enterLocalPassiveMode();

      
            log.info("开始添加水印,路径:{},文件名:{}", pathname, fileName);
            inputStream = ftpClient.retrieveFileStream(fileName);
            is = mark(inputStream, longitude,latitude,address,currentTime, Color.WHITE);

            //操作多文件时候,必须调用completePendingCommand释放,否则FTP会断开连接
            ftpClient.completePendingCommand();
            ftpClient.storeFile(attach.getAttachFilename(), is);

            ftpClient.logout();
            log.info("水印添加成功");
        } catch (Exception e) {
            log.info("水印添加失败");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }



    /**
     * 初始化ftp服务器
     */
    private static FTPClient initFtpClient() {
        FTPClient ftpClient = new FTPClient();
        ftpClient.setControlEncoding("utf-8");
        try {
            log.info("connecting...ftp服务器:" + HOST_NAME + ":" + PORT);
            //连接ftp服务器
            ftpClient.connect(HOST_NAME, PORT);
            //登录ftp服务器
            ftpClient.login(USERNAME, PASSWORD);
            //是否成功登录服务器
            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                log.info("connect failed...ftp服务器:" + HOST_NAME + ":" + PORT);
            } else {
                log.info("connect successful...ftp服务器:" + HOST_NAME + ":" + PORT);
            }
            ftpClient.setBufferSize(bufferSize);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ftpClient;
    }

方法在WINDOWS下运行,简直六到飞起,一部署到docker中,中文就乱码!!!

各种给LIUNX系统中加字体都没有解决,最后是通过Dockerfile添加字体,在容器内部直接加载字体,OK,完美解决。

首先在Dockerfile中创建fonts目录,将需要的字体放入该目录下,WINDOWS下字体在 C:\Windows\Fonts 目录下。

COPY fonts /usr/share/fonts/

RUN yum -y install fontconfig \
    && cd /usr/share/fonts \
	&& fc-cache

Dockerfile文件追加这部分内容,中文乱码完全OK~~~

最后贴张效果图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值