简体   繁体   中英

Spring file upload in a mixed form

I want to upload a file to my spring 3.0 applicatoin (created with roo).

I already have the following entity:

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

But I am not sure how to implement it on the view/controller side. Can I freely mix spring-form tags like <form:input> with normal tags like <input type=file ...> ?

I have seen the nice multipart upload section in the MVC-Documentation but still need a little help to apply it to my specific case.

Update: I think my question was badly formulated. What I wanted to do is create a spring

I found a very good explanation on how to do it in the older spring documentation and applied it to the new Spring 3.0 MVC. Basically this means you need to register a PropertyEditor in your controllers @InitBinder method. Afterwards everything will behave as expected (provided you have added MultiPartResolver to the context and set the correct form encoding). Here is my sample:

@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();        
    }    
}

有关相关的Roo问题,请参阅https://jira.springsource.org/browse/ROO-442

I don't believe you can mix-and-match file uploads with normal forms (in Spring MVC, at least), because file upload forms use the multipart/form-data encoding, rather than the usual application/x-www-form-urlencoded .

When you specify multipart/form-data , then in Spring you need to use a MultipartResolver implementation (as mentioned in the Spring docs you linked to), and all parameter decoding must go through that. Spring MVC will not be able to decode the normal form inputs, since all fields will be encoded along with the uploaded file.

It's almost certainly easier to use two separate forms, one for the normal stuff, one for the file upload.

如果您使用的是Spring 3.0,那么您可以创建一个转换器和一个Formatter(可选)而且您不必使用initBinder方法,并且保留更多POJO,但您的解决方案仍然非常有效并且仍然非常干净。

You need to have two forms, one to upload file and other to upload data. I think about this because a file is big archive or a litle and because this situation this do not work in a easy way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM