Springboot启动时在容器中创建目录已经放入Url准备好的图片

前言

package com.duodian.youhui.admin.config;

import com.duodian.youhui.admin.constants.WeChatMessageParams;
import com.duodian.youhui.admin.constants.WechatMenuParams;
import com.duodian.youhui.dao.db.wechat.WeChatBusinessNoRepository;
import com.duodian.youhui.entity.db.wechat.WeChatBusinessNo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

/**
 * @Desc:
 * @Author HealerJean
 * @Date 2018/8/17  下午5:18.
 */
@Component
public class PostConstructConfig {




    @Value("${docker_imagesDirectory}")
    private String docker_imagesDirectory ;

    @Value("${docker_erweimaEmptDirectory}")
    private String docker_erweimaEmptDirectory ;


    @Resource
    private WeChatBusinessNoRepository weChatBusinessNoRepository;


    @PostConstruct
    private void init()  throws Exception {


        //为docker容器中创建目录
        File imagesDirectory = new File(docker_imagesDirectory);
        File erweimaEmptDirectory = new File(docker_erweimaEmptDirectory);

        if(!imagesDirectory.exists()){
            imagesDirectory.mkdirs();
        }
        if(!erweimaEmptDirectory.exists()){
            erweimaEmptDirectory.mkdirs();
        }

        File shikeImage = new File(imagesDirectory,"shike.jpg");
        if(!shikeImage.exists()){
            FileOutputStream outputStreamShikeImage = new FileOutputStream(shikeImage);
            URL ushike = new URL("http://image.dangqugame.cn/admin/webproject/shike.jpg");
            BufferedImage imageShikeImage = ImageIO.read(ushike);
            ImageIO.write(imageShikeImage, "jpg", outputStreamShikeImage);

        }

        File contactmanager = new File(imagesDirectory, "contactmanager.jpg");
        if(!contactmanager.exists()) {
            FileOutputStream outputStreamContactmanager = new FileOutputStream(contactmanager);
            URL ucontactmanager = new URL("http://image.dangqugame.cn/admin/webproject/contactmanager.jpg");
            BufferedImage imageContactmanager = ImageIO.read(ucontactmanager);
            ImageIO.write(imageContactmanager, "jpg", outputStreamContactmanager);
        }
    }


}





如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦

支付包 微信 微信公众号
支付宝 微信 微信公众号
posted @ 2018-08-29 16:33  HealerJean  阅读(184)  评论(0编辑  收藏  举报