gpt4 book ai didi

java - NoClassDefFound错误: mapping files from CommonsMultipartFile to byte[]

转载 作者:行者123 更新时间:2023-12-02 00:09:29 24 4
gpt4 key购买 nike

我正在尝试使用推土机从 CommonsMultipartFile 映射到 byte[]。

我知道我需要一个 customConverter 因为 dozer 对 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>

有什么想法吗..?非常感谢!

最佳答案

如果您使用 Spring MVC,则只需在使用 @InitBinder 注解的方法中注册一个 CustomEditor 即可。

有一个ByteArrayMultipartFileEditor已经可以为您自动从 MultipartFile 转换为 byte[]

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

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

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

关于java - NoClassDefFound错误: mapping files from CommonsMultipartFile to byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13140984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com