Spring file upload in a mixed form

2019-03-15 12:31发布

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.

5条回答
相关推荐>>
2楼-- · 2019-03-15 12:56

If you are using Spring 3.0 then you can create a Converter and a Formatter(optional) And you won't have to use the initBinder method, and keep things more POJO, but your solution is still very valid and still quite clean.

查看更多
贼婆χ
3楼-- · 2019-03-15 13:05

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-15 13:11
成全新的幸福
5楼-- · 2019-03-15 13:14

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.

查看更多
淡お忘
6楼-- · 2019-03-15 13:16

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();        
    }    
}
查看更多
登录 后发表回答