使用 Spring 处理 MultipartForm [英] MultipartForm handling with Spring

查看:45
本文介绍了使用 Spring 处理 MultipartForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是用于处理上传的 RestEasy 代码:

This code is a RestEasy code for handling upload:

@Path("/fileupload")
public class UploadService {
    @POST
    @Path("/upload")
    @Consumes("multipart/form-data")
    public Response create(@MultipartForm FileUploadForm form) 
    {
       // Handle form
    }
}

使用 Spring 有没有类似的东西可以像这样处理 MultipartForm?

Is there anything similar using Spring that can handle MultipartForm just like this?

推荐答案

Spring 包含了一个依赖于 commons 的 multipartresolver-fileupload,因此要使用它,您必须将其包含在您的构建中.

Spring includes has a multipartresolver that relies on commons-fileupload, so to use it you have to include it in your build.

在您的 applicationContext.xml 中

In your applicationContext.xml

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="<max file size>"/>
</bean>

在您的控制器中,使用 org.springframework.web.multipart.MultipartFile.

In your controller, use org.springframework.web.multipart.MultipartFile.

@RequestMapping(method=RequestMethod.POST, value="/multipartexample")
public String examplePost(@RequestParam("fileUpload") MultipartFile file){
    // Handle form upload and return a view
    // ...
}

这篇关于使用 Spring 处理 MultipartForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆