2013-08-22 288 views
9

BASE64でエンコードされた文字列(encodedBytes)の形式で画像を受け取り、次の方法でサーバー側でバイト[]にデコードします。バイト配列を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

Thats cool。ありがとう兄貴。 –

+1

非常に良い解決策があなたから与えられました。この質問と回答が多くの人々に役立つことを願っています –

+0

'TransferTo'では、書き込み後にFileOutputStreamを閉じるべきですか? – Ascalonian

0

この回答は既に回答済みです。最近私はバイト配列オブジェクトをmultipartfileオブジェクトに変換するための要件に取り組んでいました。 これを達成するには2通りの方法があります。

アプローチ1:

あなたはそれを作成するためにFileDiskItemオブジェクトを使用するデフォルトCommonsMultipartFileを使用してください。 例:

Approach 1: 

は、あなたがそれを作成するためにFileDiskItemオブジェクトを使用するデフォルトCommonsMultipartFileを使用してください。 例:

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を作成し、

おかげintermediate.pdf名前で

java.io.tmpdirの にそれを格納します。

関連する問題