Spring mvc 如何将字节数组转换为多部分文件
我正在接收BASE64编码字符串(encodedBytes)形式的图像,并使用以下方法在服务器端解码为字节[]Spring mvc 如何将字节数组转换为多部分文件,spring-mvc,bytearray,multipart,Spring Mvc,Bytearray,Multipart,我正在接收BASE64编码字符串(encodedBytes)形式的图像,并使用以下方法在服务器端解码为字节[] BASE64Decoder decoder = new BASE64Decoder(); byte[] decodedBytes = decoder.decodeBuffer(encodedBytes); 现在我想用上面得到的这个字节把它转换成多部分文件 有没有办法将byte[]转换为org.springframework.web.multipart.MultipartFile???
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);
现在我想用上面得到的这个字节把它转换成多部分文件
有没有办法将byte[]转换为org.springframework.web.multipart.MultipartFile???
org.springframework.web.multipart.MultipartFile
是一个接口,因此首先需要使用此接口的实现
对于该接口,我能看到的唯一一个现成的实现是org.springframework.web.multipart.commons.commons multipartfile
。可以找到该实现的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);
}
}
/*
*
*{@link MultipartFile}接口的简单实现,用于包装已解码的字节[]
*从BASE64编码的字符串
*
*/
公共类BASE64DecodedMultipartFile实现MultipartFile{
私有最终字节[]imgContent;
public BASE64DecodedMultipartFile(字节[]imgContent){
this.imgContent=imgContent;
}
@凌驾
公共字符串getName(){
//TODO-实现取决于您的需求
返回null;
}
@凌驾
公共字符串getOriginalFilename(){
//TODO-实现取决于您的需求
返回null;
}
@凌驾
公共字符串getContentType(){
//TODO-实现取决于您的需求
返回null;
}
@凌驾
公共布尔值为空(){
返回imgContent==null | | imgContent.length==0;
}
@凌驾
公共长getSize(){
返回imgContent.length;
}
@凌驾
公共字节[]getBytes()引发IOException{
返回imgContent;
}
@凌驾
公共InputStream getInputStream()引发IOException{
返回新的ByteArrayInputStream(imgContent);
}
@凌驾
public void transferTo(File dest)抛出IOException,IllegalStateException{
新文件输出流(dest).write(imgContent);
}
}
上面已经回答了这个问题。最近,我正在研究将字节数组对象转换为多部分文件对象的需求。
有两种方法可以实现这一点
方法1:
使用默认的CommonMultipartFile,您可以在其中使用FileDiskItem对象创建它。
例如:
使用默认的CommonMultipartFile,您可以在其中使用FileDiskItem对象创建它。
例如:
方法2:
创建自己的自定义多部分文件对象,并将字节数组转换为多部分文件
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
谢谢。您提供了非常好的解决方案。希望此问题和答案对许多人有用。
如果在编写后关闭FileOutputStream,请转到
。非常好的实现。对isEmpty()方法@Override public boolean isEmpty(){return imgContent==null | | imgContent.length==0;}进行了一些小的修改,因为这似乎仍然对人们有用,所以更新以反映上面的@mommcilo注释。它非常有效!!!添加了.close()以在transferTo中扩展以避免警告@RobBlakeIn方法1 FileItem和DiskFileItem来自哪个库?@Jim-commons fileupload,最近的gradle cfg:compile group:'commons fileupload',name:'commons fileupload',version:'1.4'
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);
}
}
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);
}