2013-08-22 28 views
9

我正在以BASE64编码的字符串(encodedBytes)的形式接收图像,并使用以下方法在服务器端解码为byte []。如何将字节数组转换为MultipartFile

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

现在我想用上面得到的这个字节将它转换成MultipartFile?

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

回答

15

org.springframework.web.multipart.MultipartFile是一个接口,所以首先你需要使用这个接口的实现。

我可以看到的可以直接使用的界面的唯一实现是org.springframework.web.multipart.commons.CommonsMultipartFile。可以找到该实现的API here

或者,由于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); 
     } 
    } 
+1

这很酷。多谢兄弟。 –

+1

非常好的解决方案给你。希望这个问题和答案将会对很多人有用 –

+0

在'transferTo'中,FileOutputStream应该在写入后关闭吗? – Ascalonian

0

这个答案已经在上面回答了。最近我正在处理将字节数组对象转换为multipartfile对象的需求。 有两种方法可以实现这一点。

方法1:

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

方法2:

创建自己的自定义多文件对象,并转换为字节数组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); 
} 
} 

这个如何使用上面的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

感谢。