36

BASE64 でエンコードされた String(encodedBytes) の形式で画像を受信して​​おり、次のアプローチを使用してサーバー側で byte[] にデコードします。

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

上記で取得したこのバイトを使用して MultipartFile に変換したいですか?

byte[] を org.springframework.web.multipart.MultipartFile に変換する方法はありますか??

4

2 に答える 2

57

org.springframework.web.multipart.MultipartFileはインターフェイスであるため、最初にこのインターフェイスの実装を操作する必要があります。

すぐに使用できるそのインターフェイスについて私が見ることができる唯一の実装はorg.springframework.web.multipart.commons.CommonsMultipartFile. その実装の API は、ここにあります。

またはorg.springframework.web.multipart.MultipartFile、インターフェイスと同様に、独自の実装を提供して、バイト配列を単純にラップすることもできます。些細な例として:

/*
*<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);
        }
    }
于 2013-08-22T14:39:03.010 に答える