2013-08-22 3 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 객체로 변환하기위한 요구 사항을 처리하고 있습니다. 이 작업에는 두 가지 방법이 있습니다.

접근 1 : 당신이 그것을 만들 수 FileDiskItem 개체를 사용하는 경우

는 기본 CommonsMultipartFile를 사용합니다. 예 : 당신이 그것을 만들 수 FileDiskItem 개체를 사용하는 경우

Approach 1: 

는 기본 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을 에 그것을 저장합니다.

관련 문제