package com.lpck.recognize.utils.file;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;

/**
 * 图片转换类型工具类,从网络获取图片,将tif转换为png,存放到磁盘中
 * 引入@Value注入值,则需要将 ImageTypeConvert注入到容器中
 * 引用其方法,也要将其引入到它所依赖的类中,利用@Autowired注入
 * 否则imagePath不能获取配置文件中的值,为null
 * @Author: 
 * @Date: 2019/8/21 17:44
 * @Version 1.0.0
 * @Description
 */
@Component
public class ImageTypeConvert {

    @Value("${image.path}")
    private  String imagePath;

    /**
     * 将图片转化为base64格式;
     * @param imagePath
     * @return
     */
    public static String imageToBase64(String imagePath){
        InputStream is = null;
        byte[] data = null;
        try{
            is = new FileInputStream(imagePath);
            data = new byte[is.available()];
            is.read(data);
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        //将子节数组转化为base64形式;
        //String datas = encoder.encode(data);
        String datas = encoder.encode(data);
        //System.out.println("转化为base64:"+datas);

         //test1(datas);
        return datas;
    }

    /**
     * 转化为base64
     * @param file
     * @return
     */
    public static String imageToBase64(MultipartFile file){
            try{
                byte[] data = file.getBytes();
                BASE64Encoder encoder = new BASE64Encoder();
                String datas = encoder.encode(data);
                return datas;
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }

    }

    /**
     * 通过图片路径获取图片的字节码并将其转换为base64;
     * @param imageurl
     * @return
     * @throws Exception
     */
    public static String imageToBase642(String imageurl){
        try {
            URL url2 = new URL(imageurl);
            URLConnection conn = url2.openConnection();
            //获取输入流
            InputStream fis2 = conn.getInputStream();
            byte[] data = new byte[1024];
            int len = 0;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((len = fis2.read(data)) != -1){
                bos.write(data,0,len);
            }
            bos.close();
            byte[] data2 = bos.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            String datas2 = encoder.encode(data2);
            fis2.close();
            return datas2;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 通过图片路径获取图片的字节码并将其转换为base64;
     * @param imageurl
     * @return
     * @throws Exception
     */
    public static String imageToBase643(String imageurl){
        try {
            URL url2 = new URL(imageurl);
            URLConnection conn = url2.openConnection();
            //获取输入流
            InputStream fis2 = conn.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis2.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            fis2.close();
            byte[] data = outStream.toByteArray();
            //byte[] data = new byte[fis2.available()];
            //fis2.read(data);
            //fis2.close();
            BASE64Encoder encoder = new BASE64Encoder();
            String datas = encoder.encode(data);
            return datas;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 下载损伤图片,并将图片存放到磁盘中
     * @param file
     * @return
     */
    public String downImage(MultipartFile file){
        OutputStream os = null;
        //String disPath = "E:/javaworkspace/img/recogn/";//存放的磁盘路径
        String fileName = file.getOriginalFilename();
        String sufName = fileName.substring(fileName.lastIndexOf("."),fileName.length() );
        String retStr = "";
        try{
            String uuid = UUID.randomUUID().toString();
            String newFileName = imagePath + uuid + sufName;
            //System.out.println("磁盘路径:"+newFileName);
            retStr = uuid + sufName;
            File file1 = new File(newFileName);
            os = new FileOutputStream(file1);
            //System.out.println("是否大于0:"+file.getBytes().length);
            os.write(file.getBytes());
            os.flush();
            
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(os != null){
                    os.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  retStr;
    }

    /**
     * 通过图片路径将其下载到磁盘中
     * @param imageUrl
     * @return
     * @throws Exception
     */
    public static String brandImageDownload(String imageUrl)throws Exception{
        URL url2 = new URL(imageUrl);
        URLConnection conn = url2.openConnection();
        //获取输入流
        InputStream fis2 = conn.getInputStream();
        //下载的磁盘路径
        String fileName = imageUrl.substring(imageUrl.lastIndexOf("/"),imageUrl.length() );
        //System.out.println("图片名称:"+fileName);
        String disPath = "E:/lazyli/image/brand/"+fileName;
        File file  = new File(disPath);
        FileOutputStream fos = new FileOutputStream(file);
        byte[] data = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = fis2.read(data)) != -1){
            bos.write(data,0,len);
        }
        bos.close();
        byte[] data2 = bos.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        String datas2 = encoder.encode(data2);
        System.out.println("解析的数据:"+datas2);
        fos.write(data2);
        fis2.close();
        fos.close();
        return "success";
    }


}

  

posted on 2020-04-09 09:56  lazyli  阅读(157)  评论(0编辑  收藏  举报