36

我以 BASE64 编码字符串(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 回答
8

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

方法一:

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

Approach 1:

使用默认的 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);

方法二:

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

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);
}
}

这就是你如何使用上面的 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);
}

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

java.io.tmpdir 名称为intermediate.pdf

谢谢。

于 2017-04-20T11:36:11.830 回答