I am receiving image in the form of BASE64 encoded String(encodedBytes) and use following approach to decode into byte[] at server side.
我正在以BASE64编码字符串(encodedBytes)的形式接收图像,并使用以下方法在服务器端解码为byte[]。
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);
Now i want to convert it into MultipartFile using this byte obtained above?
现在我想用上面得到的这个字节把它转换成MultipartFile ?
Is there any way to convert byte[] to org.springframework.web.multipart.MultipartFile??
是否有办法将byte[]转换为org.springframe .web.multipart. multipartfile ?
2 个解决方案
#1
16
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。MultipartFile是一个接口,所以首先需要使用这个接口的实现。
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.springframe .web.multipart.common . commonsmultipartfile。该实现的API可以在这里找到
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:
另外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);
}
}
#2
1
This answer has already been answered above. Recently i was working on the requirement to convert byte array object to multipartfile object. There are two ways to achieve this.
这个答案已经在上面回答过了。最近,我正在研究将字节数组对象转换为multipartfile对象的需求。有两种实现方法。
Approach 1:
方法1:
Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example:
使用默认的CommonsMultipartFile,使用FileDiskItem对象创建它。例子:
Approach 1:
Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example:
使用默认的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);
Approach 2:
方法2:
Create your own custom multipart file object and convert the byte array to multipartfile.
创建您自己的自定义多部分文件对象,并将字节数组转换为多部分文件。
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);
}
}
This how you can use above CustomMultipartFile object.
这就是如何使用上面的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);
}
This will create the required PDF and store that into
这将创建所需的PDF并将其存储到
java.io.tmpdir with the name intermediate.pdf
. io .名称为medium .pdf的tmpdir
Thanks.
谢谢。
#1
16
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。MultipartFile是一个接口,所以首先需要使用这个接口的实现。
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.springframe .web.multipart.common . commonsmultipartfile。该实现的API可以在这里找到
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:
另外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);
}
}
#2
1
This answer has already been answered above. Recently i was working on the requirement to convert byte array object to multipartfile object. There are two ways to achieve this.
这个答案已经在上面回答过了。最近,我正在研究将字节数组对象转换为multipartfile对象的需求。有两种实现方法。
Approach 1:
方法1:
Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example:
使用默认的CommonsMultipartFile,使用FileDiskItem对象创建它。例子:
Approach 1:
Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example:
使用默认的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);
Approach 2:
方法2:
Create your own custom multipart file object and convert the byte array to multipartfile.
创建您自己的自定义多部分文件对象,并将字节数组转换为多部分文件。
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);
}
}
This how you can use above CustomMultipartFile object.
这就是如何使用上面的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);
}
This will create the required PDF and store that into
这将创建所需的PDF并将其存储到
java.io.tmpdir with the name intermediate.pdf
. io .名称为medium .pdf的tmpdir
Thanks.
谢谢。