NoClassDefFoundError:将文件从CommonsMultipartFile映射到byte []

时间:2012-10-30 14:20:32

标签: java spring portlet dozer

我正在尝试使用Dozer从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:org / apache / commons / fileupload / FileUploadException

这很奇怪,因为我已经在我的pom.xml文件中定义了依赖项......

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

任何想法......?非常感谢!

1 个答案:

答案 0 :(得分:3)

如果你正在使用Spring MVC,你可以在用CustomEditor注释的方法中注册@InitBinder

已有ByteArrayMultipartFileEditor可用,可以为您MultipartFile自动转换为byte[]

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

您的域对象/表单可以直接保留byte[]而不是MultipartFile

我相信你也可以使用Spring Portlet MVC做同样的事情。