博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转载 spring mvc-multipart
阅读量:5912 次
发布时间:2019-06-19

本文共 3554 字,大约阅读时间需要 11 分钟。

hot3.png

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.

            Upload a file please                

Please upload a file

spring-mvc.xml

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

Controller

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

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

@Controllerpublic 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:

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

            Upload a file please                

Please upload a file

spring-mvc.xml

Controller

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

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

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

 

 

转载于:https://my.oschina.net/yizhichao/blog/739590

你可能感兴趣的文章
PHP源码目录结构
查看>>
Linux桌面虚拟化技术KVM介绍及其安装
查看>>
硬盘主引导记录详解
查看>>
2017-12-19 Linux学习笔记
查看>>
用户与用户组管理
查看>>
CentOS 6.8 手工安装 Firefox
查看>>
【栈】POJ 1028 Web Navigation
查看>>
[文摘]JDK里的设计模式
查看>>
初学大数据需要了解哪些方面的知识?
查看>>
能量山水画家武湲承:搬运时空能量,聚藏山水画中
查看>>
谈一次异步上传到又拍云的案例教程
查看>>
【Ubuntu】IBM资料学习笔记
查看>>
C#接收命令行参数的代码
查看>>
xcode svn的操作(1)
查看>>
判断ip的有效性---boost
查看>>
android activity之间传递对象
查看>>
最全Handler源码解剖
查看>>
史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
查看>>
大流量、高负载场景 Nginx+Linux 性能调优
查看>>
华为发布全球首款Android 3.2平板电脑
查看>>