springboot 图片转base64格式(可以直接显示的base64格式)

话不多说上代码

    public static byte[] getImage(String imagePath)
    {
        InputStream is = getFile(imagePath);
        try
        {
            return IOUtils.toByteArray(is);
        }
        catch (Exception e)
        {
            log.error("图片加载异常 {}", e);
            return null;
        }
        finally
        {
            IOUtils.closeQuietly(is);
        }
    }
    public static String byteToBase64(byte[] b) {
        try {
            return Base64.getEncoder().encodeToString(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  public static String getFileType(File file)
    {
        if (null == file)
        {
            return StringUtils.EMPTY;
        }
        return getFileType(file.getName());
    }

调用

       if (StringUtils.isEmpty(url)) {
            return null;
        }
        byte[] image = getImage(url);
        String s = byteToBase64(image);
        File file = new File(url);
        String fileType =getFileType(file);
        String images= "data:image/"+fileType+";base64,"+s;
        return images;
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Spring Boot实现上传图片换为base64格式的代码: ```java import java.io.IOException; import java.util.Base64; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller public class ImageController { @PostMapping(value = "/upload", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }) public ResponseEntity<String> uploadImage(@RequestParam("image") MultipartFile file) throws IOException { if (file.isEmpty()) { return ResponseEntity.badRequest().body("Please select a file"); } String fileName = StringUtils.cleanPath(file.getOriginalFilename()); byte[] bytes = file.getBytes(); String encodedString = Base64.getEncoder().encodeToString(bytes); return ResponseEntity.ok().body(encodedString); } } ``` 该代码创建了一个名为 `ImageController` 的Spring Boot控制器,其包含了一个名为 `uploadImage` 的POST请求处理方法。该方法接收一个名为 "image" 的文件参数,将上传的文件换为base64编码字符串并返回。请将代码的 "/upload" 替换为你实际的请求路径。 在使用前,你需要在 `pom.xml` 文件添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 同时,你还需要在Spring Boot应用程序的配置文件添加以下配置项: ```properties spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=20MB spring.servlet.multipart.max-request-size=20MB ``` 这些配置项将启用文件上传,并设置了上传文件的最大大小。请根据你的实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值