如何将字节数组转换为MultipartFile

Sho*_*ate 17 bytearray spring-mvc multipart

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

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);
Run Code Online (Sandbox Code Playgroud)

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

有没有办法将byte []转换为org.springframework.web.multipart.MultipartFile?

Rob*_*ake 29

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 您给出的解决方案非常好。希望这个问题和答案对很多人有用 (2认同)

小智 7

这个答案已经在上面回答了。最近我正在研究将字节数组对象转换为多部分文件对象的要求。有两种方法可以实现这一点。

方法一:

使用默认的 CommonsMultipartFile,您可以在其中使用 FileDiskItem 对象来创建它。例子:

Approach 1:
Run Code Online (Sandbox Code Playgroud)

使用默认的 CommonsMultipartFile,您可以在其中使用 FileDiskItem 对象来创建它。例子:

FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile.getName(), 100000000, new java.io.File(System.getProperty("java.io.tmpdir")));              
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
Run Code Online (Sandbox Code Playgroud)

方法二:

创建您自己的自定义多部分文件对象并将字节数组转换为多部分文件。

public class CustomMultipartFile implements MultipartFile {

private final byte[] fileContent;

private String fileName;

private String contentType;

private File file;

private String destPath = System.getProperty("java.io.tmpdir");

private FileOutputStream fileOutputStream;

public CustomMultipartFile(byte[] fileData, String name) {
    this.fileContent = fileData;
    this.fileName = name;
    file = new File(destPath + fileName);

}

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

public void clearOutStreams() throws IOException {
if (null != fileOutputStream) {
        fileOutputStream.flush();
        fileOutputStream.close();
        file.deleteOnExit();
    }
}

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

@Override
public InputStream getInputStream() throws IOException {
    return new ByteArrayInputStream(fileContent);
}
}
Run Code Online (Sandbox Code Playgroud)

这是您如何使用上面的 CustomMultipartFile 对象。

String fileName = "intermediate.pdf";
CustomMultipartFile customMultipartFile = new CustomMultipartFile(bytea, fileName);
try {
customMultipartFile.transferTo(customMultipartFile.getFile());

} catch (IllegalStateException e) {
    log.info("IllegalStateException : " + e);
} catch (IOException e) {
    log.info("IOException : " + e);
}
Run Code Online (Sandbox Code Playgroud)

这将创建所需的 PDF 并将其存储到

java.io.tmpdir,名称为intermediate.pdf

谢谢。

  • @Jim - commons-fileupload,最近的 gradle cfg:编译组:'commons-fileupload',名称:'commons-fileupload',版本:'1.4' (2认同)