java Spring文件混合上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2190851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:57:00  来源:igfitidea点击:

Spring file upload in a mixed form

javaspringfile-uploadspring-mvc

提问by er4z0r

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

我想将文件上传到我的 spring 3.0 应用程序(使用 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 ...>?

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

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

我在 MVC 文档中看到了不错的分段上传部分,但仍需要一些帮助才能将其应用于我的特定案例。

采纳答案by er4z0r

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:

我在旧的 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();        
    }    
}

回答by skaffman

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-dataencoding, rather than the usual application/x-www-form-urlencoded.

我不相信您可以将文件上传与普通表单(至少在 Spring MVC 中)混合搭配,因为文件上传表单使用multipart/form-data编码,而不是通常的application/x-www-form-urlencoded.

When you specify multipart/form-data, then in Spring you need to use a MultipartResolverimplementation (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.

当您指定 时multipart/form-data,则在 Spring 中您需要使用一个 MultipartResolver实现(如您链接到的 Spring 文档中所述),并且所有参数解码都必须通过该实现。Spring MVC 将无法解码普通表单输入,因为所有字段都将与上传的文件一起编码。

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

使用两种单独的表单几乎肯定更容易,一种用于普通内容,一种用于文件上传。

回答by Ben Alex

See https://jira.springsource.org/browse/ROO-442for the related Roo issue.

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

回答by Mark

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.

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

回答by JeanKBN

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.

您需要有两种表单,一种用于上传文件,另一种用于上传数据。我想到这一点是因为文件是大档案或很小的文件,并且因为这种情况这并不容易。