java_图片转base64字符串

1、直接获取文件,转based4

/**
     * 2017-7-17
     * @param id
     * @return
     */
    @RequestMapping("add")
    public void add(CqscBrayData cqscBrayData,MultipartHttpServletRequest request,
            HttpServletResponse response){
        response.setContentType("text/html; charset=utf-8");
        MultipartFile fileCrl = request.getFile("myUploadCrl");
        CommonsMultipartFile cf= (CommonsMultipartFile)fileCrl; 
        DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
        File f = fi.getStoreLocation();
        if(fi.getSize() != 0){
            byte[] bs = null;
            try {
                bs = FileUtils.readFileToByteArray(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 加密为base64编码字符串
            BASE64Encoder encoder = new BASE64Encoder();
            String bsCrl = encoder.encode(bs);
            System.out.println(bsCrl);
            cqscBrayData.setCrlBase64(bsCrl);
        }

        try {
            om.writeValue(response.getWriter(), map);
            response.getWriter().flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                response.getWriter().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

2、从本地获取文件,转based4

public static String getBase64Path(String img_path){
        String photopath = PropertiesUtil.getProp("photoPath");
        String replac = photopath.substring(3);
        String path = photopath.replace(replac, img_path);
        File file = new File(path);
        byte[] bs = null;
        if(file.exists()){
            try {
                bs = FileUtils.readFileToByteArray(file);
                // 加密为base64编码字符串
                BASE64Encoder encoder = new BASE64Encoder();
                String baseStr = encoder.encode(bs);
                System.out.println(baseStr);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return path;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的Base64类来实现将图片Base64字符串的功能。以下是示例代码: ```java import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; public class ImageToBase64 { public static void main(String[] args) { String imagePath = "path/to/image.jpg"; String base64Image = getImageAsBase64(imagePath); System.out.println(base64Image); } public static String getImageAsBase64(String imagePath) { String base64Image = ""; File file = new File(imagePath); try (FileInputStream imageInFile = new FileInputStream(file)) { // Reading a Image file from file system byte imageData[] = new byte[(int) file.length()]; imageInFile.read(imageData); // Converting Image byte array into Base64 String base64Image = Base64.getEncoder().encodeToString(imageData); } catch (IOException e) { System.out.println("Exception while reading the Image " + e); } return base64Image; } } ``` 在示例代码中,首先定义了一个getImageAsBase64方法,该方法接收一个图片文件路径,返回换后的Base64字符串。 在getImageAsBase64方法中,首先创建了一个FileInputStream对象来读取图片文件,将图片数据读入到一个byte数组中,然后使用Java 8中的Base64类将byte数组换成Base64字符串,最后返回该字符串。 需要注意的是,在本示例中使用的是Java 8中的Base64类,如果你使用的是Java 7或更早的版本,可以使用第三方库(如apache commons codec)来实现相同的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值