以混合形式上传Spring文件

er4*_*z0r 11 java spring file-upload spring-mvc

我想上传一个文件到我的spring 3.0 applicatoin(用roo创建).

我已经拥有以下实体:

@Entity
@RooJavaBean
@RooToString
@RooEntity
public class SelniumFile {

    @ManyToOne(targetEntity = ShowCase.class)
    @JoinColumn
    private ShowCase showcase;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    private byte[] file;

    @NotNull
    private String name;
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在视图/控制器端实现它.我可以自由混合弹簧形式的标签,如<form:input>普通标签<input type=file ...>吗?

我已经在MVC文档中看到了很好的分段上传部分,但仍然需要一点帮助才能将它应用到我的特定情况.

er4*_*z0r 7

更新:我认为我的问题制定得很糟糕.我想做的是创造一个弹簧

我在旧的spring文档中找到了一个非常好的解释,并将其应用到新的Spring 3.0 MVC中.基本上这意味着您需要在控制器@InitBinder方法中注册PropertyEditor.之后一切都将按预期运行(前提是您已将MultiPartResolver添加到上下文并设置正确的表单编码).这是我的样本:

@RequestMapping("/scriptfile/**")
@Controller
public class ScriptFileController {

    //we need a special property-editor that knows how to bind the data
    //from the request to a byte[]
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    }

    @RequestMapping(value = "/scriptfile", method = RequestMethod.POST)    
    public String create(@Valid ScriptFile scriptFile, BindingResult result, ModelMap modelMap) {    
        if (scriptFile == null) throw new IllegalArgumentException("A scriptFile is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("scriptFile", scriptFile);            
            modelMap.addAttribute("showcases", ShowCase.findAllShowCases());            
            return "scriptfile/create";            
        }        
        scriptFile.persist();        
        return "redirect:/scriptfile/" + scriptFile.getId();        
    }    
}
Run Code Online (Sandbox Code Playgroud)