2012-10-30 3 views
0

도너와 CommonsMultipartFile에서 byte []로 매핑하려고합니다.NoClassDefFoundError : CommonsMultipartFile에서 byte []에 파일 매핑하기

나는 내가 customConverter 원인 도저는 CommonsMultipartFile 유형에 대해 아무것도 모르는 필요 알고, 그래서 내가 만든이 :

public class FileJtfConverter extends DozerConverter<CommonsMultipartFile, byte[]> { 

    /** 
    * Constructor 
    */ 
    public FileJtfConverter() { 
     super(CommonsMultipartFile.class, byte[].class); 
    } 

    @Override 
    public final byte[] convertTo(CommonsMultipartFile a, byte[] b) { 
     if (a != null) { 
      return a.getBytes(); 
     } 
     return null; 
    } 

    @Override 
    public final CommonsMultipartFile convertFrom(byte[] b, CommonsMultipartFile a) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
    } 

그리고 내 도저 XML 파일 :

<mapping type="one-way">   
    <class-a>myPackage.ClassA 
    </class-a> 
    <class-b>myPackage.ClassB 
    </class-b> 
    ... 
    <field custom-converter="es.xunta.formacion.sifo3.transporte.util.converter.FileJtfConverter"> 
     <a>anexo</a> 
     <b>anexo</b> 
    </field> 
</mapping> 

경우 클래스 A와 B 클래스는 다음과 같습니다.

public class ClassA{ 
    ... 
    private CommonsMultipartFile anexo; 
    ... 
    public final CommonsMultipartFile getAnexo() { 
    return anexo; 
    } 

    public final void setAnexo(CommonsMultipartFile anexo) { 
    this.anexo = anexo; 
    } 
} 

public class ClassB{ 
    ... 
    protected byte[] anexo; 
    ... 
    public void setAnexo(byte[] value) { 
    this.anexo = ((byte[]) value); 
    } 

    public byte[] getAnexoPago() { 
     return anexoPago; 
    } 
} 

모두 괜찮아 보이지만 예외가 있습니다. org.dozer.MappingException : java.lang.NoClassDefFoundError가 : 조직/아파치/평민/파일 업로드/FileUploadException

그리고 내 pom.xml 파일의 종속성을 정의하기 때문에이 꽤 이상해 ...

<dependency> 
     <groupId>commons-fileupload</groupId> 
     <artifactId>commons-fileupload</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>commons-io</groupId> 
     <artifactId>commons-io</artifactId>     
    </dependency> 

어떤 아이디어 ..? 고마워요!

+0

클래스 경로에 commons-fileupload를 추가하십시오. – bmargulies

답변

3

스프링 MVC를 사용하는 경우 @InitBinder 주석이 달린 메소드에 CustomEditor을 등록하면됩니다.

MultipartFile에서 byte[]으로 자동 변환 할 수있는 ByteArrayMultipartFileEditor이 이미 있습니다.

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); 
} 

도메인 개체/형태는 직접 byte[] 대신 MultipartFile 저장할 수 있습니다.

스프링 포틀릿 MVC에서도 동일한 작업을 수행 할 수 있다고 생각합니다.

+0

좋은데, MultipartFile이 아닌 CommonsMultipartFile을 사용하고 있습니다. CommonsMultipartFile에 대해 이와 같은 것이 있습니까? – elcadro

+0

'CommonsMultipartFile'은'MultipartFile'을 구현합니다. 그냥 시도해보십시오, 효과가있을 수 있습니다. – adarshr

+0

이 방법을 시도했지만 작동하지 않습니다. 이 initBinder를 사용하는 경우 개인 Classons 매개 변수를 개인용 CommonsMultipartFile anexo에서 개인 바이트 [] anexo로 변경해야합니다. 그렇지 않아야합니까? 그건 정확히 내가하려고하는 것이 아니지만 작동 할 수 있습니다 ... – elcadro