文档章节

转载 spring mvc-multipart

yizhichao
 yizhichao
发布于 2016/08/29 23:58
字数 413
阅读 7
收藏 0

spring mvc

1) Using a MultipartResolver with Commons FileUpload

The following example shows how to use the CommonsMultipartResolver:

Of course you also need to put the appropriate jars in your classpath for the multipart resolver to work. In the case of the CommonsMultipartResolver, you need to usecommons-fileupload.jar.

When the Spring DispatcherServlet detects a multi-part request, it activates the resolver that has been declared in your context and hands over the request. The resolver then wraps the current HttpServletRequest into a MultipartHttpServletRequest that supports multipart file uploads. Using theMultipartHttpServletRequest, you can get information about the multiparts contained by this request and actually get access to the multipart files themselves in your controllers.

<html>
    <head>
        <title>Upload a file please</title>
    </head>
    <body>
        <h1>Please upload a file</h1>
        <form method="post" action="/form" enctype="multipart/form-data">
            <input type="text" name="name"/>
            <input type="file" name="file"/>
            <input type="submit"/>
        </form>
    </body>
</html>

spring-mvc.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="100000"/>

</bean>

---------------------------------------------------------------------------------------------------------------------------------------------

Controller

@PostMapping("/device")
public String onSubmit(@RequestPart("file") MultipartFile file) {

    // ...

}

---------------------------------------------------------------------------------------------------------------------------------------------

@Controller
public class FileUploadController {

    @PostMapping("/form")
    public String handleFormUpload(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
            return "redirect:uploadSuccess";
        }

        return "redirect:uploadFailure";
    }

}

2) Using a MultipartResolver with Servlet 3.0

In order to use Servlet 3.0 based multipart parsing, you need to mark the DispatcherServlet with a "multipart-config" section in web.xml, or with ajavax.servlet.MultipartConfigElement in programmatic Servlet registration, or in case of a custom Servlet class possibly with ajavax.servlet.annotation.MultipartConfig annotation on your Servlet class. Configuration settings such as maximum sizes or storage locations need to be applied at that Servlet registration level as Servlet 3.0 does not allow for those settings to be done from the MultipartResolver.

Once Servlet 3.0 multipart parsing has been enabled in one of the above mentioned ways you can add the StandardServletMultipartResolver to your Spring configuration:

----------------------------------------------------------------------------------------------------------------------------------------------

<html>
    <head>
        <title>Upload a file please</title>
    </head>
    <body>
        <h1>Please upload a file</h1>
        <form method="post" action="/form" enctype="multipart/form-data">
            <input type="text" name="name"/>
            <input type="file" name="file"/>
            <input type="submit"/>
        </form>
    </body>
</html>

spring-mvc.xml

<bean id="multipartResolver"
        class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>

Controller

----------------------------------------------------------------------------------------------------------------------------------------------

@Controller
public class FileUploadController {

    @PostMapping("/form")
    public String handleFormUpload(@RequestParam("name") String name,
            @RequestParam("file") Part file) {

        InputStream inputStream = file.getInputStream();
        // store bytes from uploaded file somewhere

        return "redirect:uploadSuccess";
    }

}

----------------------------------------------------------------------------------------------------------------------------------------------

 

 

本文转载自:

共有 人打赏支持
yizhichao

yizhichao

粉丝 14
博文 463
码字总数 362827
作品 0
南京
程序员
私信 提问
Spring中formdata方式提交json对象和file之一

问题 想使用vue-resource表单提交方式到spring,这个表单中包含json对象和file对象。 思路 将json对象和file对象都放到表单提交方式里面的中,并在中指定内容格式。 步骤 Spring实现和配置 ...

亚林瓜子
2018/06/12
0
0
Spring MVC 实践 - Component

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hanqing280441589/article/details/51831979 标签 : Java与Web Converter Spring MVC的数据绑定并非没有任何...

菜鸟-翡青
2016/07/05
0
0
Spring boot文件上传

Spring boot PostMan application.properties 参考 Uploading Files Spring MVC Multiple File Upload Example...

亚林瓜子
03/06
0
0
Spring Framework 多个安全漏洞预警

4月5日,Pivotal 发布了 Spring Framework 存在多个安全漏洞的公告: (1)spring-messaging 模块远程代码执行漏洞 对应CVE编号:CVE-2018-1270 漏洞公告链接:https://pivotal.io/security...

h4cd
2018/04/07
7.9K
7
SpringMVC处理multipart请求.

一、简述 multipart格式的数据会将一个表单拆分为多个部分(part),每个部分对应一个输入域。在一般的表单输入域中,它所对应的部分中会放置文本型数据,但是如果上传文件的话,它所对应的部...

jmcui
2018/01/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

js算法总结

数列求和 等差数列求和 function sum(a0,d,n){//a0->首项,d->公差,n->项数//(首项+末项)*项数/2return (a1+(a1+d*n))*n/2;} 等比数列求和 function sum(a0,q,n){//a0->首项,q->公......

祖达
33分钟前
1
0
小白?转型?毕业生?外行学习快速入行大数据开发指南

这篇文章中,本文将针对三种不同的、想要进入数据科学领域的人群,给出自己的经验,帮助他们迅速有效入行。 虽然没有适合每个人的万能解决方案,但这三类建议值得想转行的你一看。 第1类:新...

董黎明
40分钟前
1
0
好文 | MySQL 索引B+树原理,以及建索引的几大原则

MySQL事实上使用不同的存储引擎也是有很大区别的,下面猿友们可以了解一下。 一、存储引擎的比较 注:上面提到的B树索引并没有指出是B-Tree和B+Tree索引,但是B-树和B+树的定义是有区别的。 ...

Java爬坑之路
43分钟前
1
0
mysql group by 和 Order By 执行顺序

1.在写统计的时候,我们会用到统计首单,这样里面设计到排序。写子查询的方式当然可以实现, 但是我们有时候,需要创建视图,视图不支持带子查询的。 加了排序后会返回,排序后的哪个第一条数...

kuchawyz
48分钟前
2
0
Spring Boot 2.X 如何添加拦截器?

最近使用SpringBoot2.X搭建了一个项目,大部分接口都需要做登录校验,所以打算使用注解+拦截器来实现,在此记录下实现过程。 一、实现原理 1. 自定义一个注解@NeedLogin,如果接口需要进行登...

花漾年华
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部