zoukankan      html  css  js  c++  java
  • Spring MVC 使用MultipartResolver与Commons FileUpload传输文件

    配置MultipartResolver:用于处理表单中的file

    <!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->  
        <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
            p:defaultEncoding="UTF-8"  
            p:maxUploadSize="5400000"  
            p:uploadTempDir="fileUpload/temp"  
         >  
    </beans:bean>
    
    当然,要让多路解析器正常工作,你需要在classpath路径下准备必须的jar包。如果使用的是通用的多路上传解析器CommonsMultipartResolver,你所需要的jar包是commons-fileupload.jar。
    当Spring的DispatcherServlet检测到一个多部分请求时,它会激活你在上下文中声明的多路解析器并把请求交给它。解析器会把当前的HttpServletRequest请求对象包装成一个支持多路文件上传的请求对象MultipartHttpServletRequest。有了MultipartHttpServletRequest对象,你不仅可以获取该多路请求中的信息,还可以在你的控制器中获得该多路请求的内容本身。
    defaultEncoding="UTF-8" 是请求的编码格式,默认为iso-8859-1
    maxUploadSize="5400000" 是上传文件的大小,单位为字节
    uploadTempDir="fileUpload/temp" 为上传文件的临时路径

    Spring MVC 处理表单中的文件上传

    当解析器MultipartResolver完成处理时,请求便会像其他请求一样被正常流程处理。首先,创建一个接受文件上传的表单将允许用于直接上传整个表单。编码属性(enctype="multipart/form-data")能让浏览器知道如何对多路上传请求的表单进行编码(encode)。注意要在form标签中加上enctype="multipart/form-data"表示该表单是要处理文件的,这是最基本的东西,很多人会忘记然而当上传出错后则去找程序的错误,却忘了这一点
    <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>
    
    下一步是创建一个能处理文件上传的控制器。这里需要的控制器与一般注解了@Controller的控制器基本一样,除了它接受的方法参数类型是MultipartHttpServletRequest,或MultipartFile。
    @Controller
    public class FileUploadController {
    
        @RequestMapping(path = "/form", method = RequestMethod.POST)
        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";
        }
    
    }
    
    请留意@RequestParam注解是如何将方法参数对应到表单中的定义的输入字段的。在上面的例子中,我们拿到了byte[]文件数据,只是没对它做任何事。在实际应用中,你可能会将它保存到数据库、存储在文件系统上,或做其他的处理。
    当使用Servlet 3.0的多路传输转换时,你也可以使用javax.servlet.http.Part作为方法参数:
    @Controller
    public class FileUploadController {
    
        @RequestMapping(path = "/form", method = RequestMethod.POST)
        public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") Part file) {
    
            InputStream inputStream = file.getInputStream();
            // store bytes from uploaded file somewhere
    
            return "redirect:uploadSuccess";
        }
    
    }
    
    到此基本的文件上传就结束了。
    MultipartFile类常用的一些方法:
    String getContentType()//获取文件MIME类型
    InputStream getInputStream()//后去文件流
    String getName() //获取表单中文件组件的名字
    String getOriginalFilename() //获取上传文件的原名
    long getSize()  //获取文件的字节大小,单位byte
    boolean isEmpty() //是否为空
    void transferTo(File dest) //保存到一个目标文件中。
    
  • 相关阅读:
    使用图形化界面打包自己的类库
    搭建自己的NuGet服务器,上传自定义NuGet包
    在内部架设NuGet服务器
    Prism简介
    Nhibernate Icreteria 分页查询
    uDig介绍
    基于Geoserver配置多图层地图以及利用uDig来进行样式配置
    如何在GeoServer上发布一张地图
    XML的SelectNodes使用方法以及XPath
    coded ui run in interactive mode
  • 原文地址:https://www.cnblogs.com/lanqingyu/p/10400961.html
Copyright © 2011-2022 走看看