根据图片 url 链接将图片转为 base64

根据文件链接将文件下载到本地

 // 根据链接下载图片到服务器
    public String downloadPic(String url){
        // 随机生成名字
        //生成uuid
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        String imgName=uuid;
        // 本地测试使用路径
        String path1="D:\\testFile\\";
      
        File tempFile = new File(path1);
         // 文件夹不存在就新建文件夹
        if(!tempFile.exists()){
            tempFile.mkdirs();
        }
        // 本地测试使用
        String path=path1+"\\"+imgName+".jpg";

        try{
            URL url1 = new URL(url);
            DataInputStream dataInputStream = new DataInputStream(url1.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

			// 文件最大为1024
            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) >0){
                output.write(buffer,0,length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();

        }catch (MalformedURLException | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

将图片转换为base64

// 图片转为base64
    public String picToBase64(String imgFile){
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();

        //System.out.println("encoder:"+encoder);
        return encoder.encode(data);
    }

根据图片链接将图片转为base64

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import sun.misc.BASE64Encoder;
import java.io.*;


@Component
public class PicToBase64Utils {


    @Autowired
    private RestTemplate restTemplate;

    // 根据链接将图片转为base64
    public String PicToBase64(String url){

        byte[] data = null;

        try{

            ResponseEntity<Resource> resourceResponseEntity = restTemplate.getForEntity(url, Resource.class);
            InputStream inputStream = resourceResponseEntity.getBody().getInputStream();

            data = new byte[inputStream.available()];
            inputStream.read(data);

            inputStream.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值