0

I'm trying to map with dozer from CommonsMultipartFile to byte[].

I know I need a customConverter cause dozer doesn't know anything about CommonsMultipartFile type, so I made it:

    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.");
    }
    }

And my dozer xml file:

<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>

Where Class A and class B are:

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;
    }
}

All seems ok, but it's throwing an exception: org.dozer.MappingException: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException

And this is pretty weird, because I have defined the dependencies in my pom.xml file...

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

Any ideas..? Thanks a lot!

elcadro
  • 1,482
  • 3
  • 21
  • 45

1 Answers1

3

If you're using Spring MVC, you could just register a CustomEditor in your method annotated with @InitBinder.

There is a ByteArrayMultipartFileEditor already available that can do automatic conversion from MultipartFile to byte[] for you.

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

Your domain object / form can directly hold byte[] instead of MultipartFile.

I believe you could do the same with Spring Portlet MVC as well.

adarshr
  • 61,315
  • 23
  • 138
  • 167
  • Sounds good, but I'm using a CommonsMultipartFile, not a MultipartFile. Is there anything like this for the CommonsMultipartFile? – elcadro Oct 30 '12 at 15:49
  • `CommonsMultipartFile` implements `MultipartFile`. Just try it, it might work. – adarshr Oct 30 '12 at 15:56
  • I've tried this way, but it doesn't work. If I use this initBinder, I should changue my ClassA parameter from private CommonsMultipartFile anexo to private byte[] anexo, shouldn't I? That's not exactly what I'm trying to do, but it could work... – elcadro Oct 30 '12 at 16:23
  • Yes, well you could get rid of dozer and any other conversion logic that you may have written specific to this. – adarshr Oct 30 '12 at 16:29
  • 1
    Ok, It works. It wasn't exactly what I was looking for, and I had to change some stuff, but finally it works. Thanks a lot! – elcadro Nov 02 '12 at 13:29