如何将字节数组转换为MultipartFile [英] How to convert byte array to MultipartFile

查看:497
本文介绍了如何将字节数组转换为MultipartFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接收BASE64编码的String(encodedBytes)形式的图像,并使用以下方法在服务器端将其解码为byte [].

I am receiving image in the form of BASE64 encoded String(encodedBytes) and use following approach to decode into byte[] at server side.

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);

现在我想使用上面获得的此字节将其转换为MultipartFile吗?

Now i want to convert it into MultipartFile using this byte obtained above?

有什么方法可以将byte []转换为org.springframework.web.multipart.MultipartFile?

Is there any way to convert byte[] to org.springframework.web.multipart.MultipartFile??

推荐答案

org.springframework.web.multipart.MultipartFile是一个接口,因此首先您需要使用该接口的实现.

org.springframework.web.multipart.MultipartFile is an interface so firstly you are going to need to work with an implementation of this interface.

对于该接口,我可以看到的唯一可以立即使用的实现是org.springframework.web.multipart.commons.CommonsMultipartFile.可以在

The only implementation that I can see for that interface that you can use out-of-the-box is org.springframework.web.multipart.commons.CommonsMultipartFile. The API for that implementation can be found here

或者,由于org.springframework.web.multipart.MultipartFile是一个接口,因此您可以提供自己的实现,只需包装字节数组即可.作为一个简单的例子:

Alternatively as org.springframework.web.multipart.MultipartFile is an interface, you could provide your own implementation and simply wrap your byte array. As a trivial example:

/*
*<p>
* Trivial implementation of the {@link MultipartFile} interface to wrap a byte[] decoded
* from a BASE64 encoded String
*</p>
*/
public class BASE64DecodedMultipartFile implements MultipartFile {
        private final byte[] imgContent;

        public BASE64DecodedMultipartFile(byte[] imgContent) {
            this.imgContent = imgContent;
        }

        @Override
        public String getName() {
            // TODO - implementation depends on your requirements 
            return null;
        }

        @Override
        public String getOriginalFilename() {
            // TODO - implementation depends on your requirements
            return null;
        }

        @Override
        public String getContentType() {
            // TODO - implementation depends on your requirements
            return null;
        }

        @Override
        public boolean isEmpty() {
            return imgContent == null || imgContent.length == 0;
        }

        @Override
        public long getSize() {
            return imgContent.length;
        }

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

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

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

这篇关于如何将字节数组转换为MultipartFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆