Java BufferedImage转换为MultipartFile

导读:本篇文章讲解 Java BufferedImage转换为MultipartFile,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Java里读取图片或调整图片大小可使用BufferedImage进行操作(参考我另一篇文章Java修改图片大小尺寸),但有时候我们需要将BufferedImage转为MultipartFile进行其他操作可如下转换:

方法一:

1.新建ConvertToMultipartFile类实现MultipartFile接口

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class ConvertToMultipartFile implements MultipartFile {
    private byte[] fileBytes;
    String name;
    String originalFilename;
    String contentType;
    boolean isEmpty;
    long size;

    public ConvertToMultipartFile(byte[] fileBytes, String name, String originalFilename, String contentType,
                          long size) {
        this.fileBytes = fileBytes;
        this.name = name;
        this.originalFilename = originalFilename;
        this.contentType = contentType;
        this.size = size;
        this.isEmpty = false;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getOriginalFilename() {
        return originalFilename;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return isEmpty;
    }

    @Override
    public long getSize() {
        return size;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return fileBytes;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(fileBytes);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(fileBytes);
    }
}

2.BufferedImage 转换为 MultipartFile

BufferedImage 先转为byte[ ],再通过上面的ConvertToMultipartFile类转为MultipartFile

	try {
            //读取图片转换为 BufferedImage
            BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg"));
            //BufferedImage 转化为 ByteArrayOutputStream
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", out);
            //ByteArrayOutputStream 转化为 byte[]
            byte[] imageByte = out.toByteArray();
            //将 byte[] 转为 MultipartFile
            MultipartFile multipartFile = new ConvertToMultipartFile(imageByte, "newNamepic", "pic1", "jpg", imageByte.length);
      } catch (IOException e) {
            e.printStackTrace();
      }

方法二:

引入依赖:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.3.2</version>
        <scope>compile</scope>
    </dependency>
	try {
 			//读取图片转换为 BufferedImage
            BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg"));
            //调整图片大小后的BufferedImage。resizeImage方法是调整图片大小的可参考文章开头我上一篇文章
            BufferedImage newImage = ImageUtils.resizeImage(image, 200, 200);
            //将newImage写入字节数组输出流
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write( newImage, "jpg", baos );
            
			//转换为MultipartFile 
            MultipartFile multipartFile = new MockMultipartFile("pic1.jpg", baos.toByteArray());
       } catch (IOException e) {
            e.printStackTrace();
       }

参考:
How to convert BufferedImage to a MultiPart file without saving file to disk?

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/80190.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!