一、概述

  http请求项目搭建:地址https://github.com/bjlhx15/common.git 中的spring-framework-core 的 spring-http-XX相关

  主要针对post请求中的,form表单【application/x-www-form-urlcoded】提交,json【application/json】提交,文件【multipart/form-data】提交

  详细参看:005-四种常见的 POST 提交数据方式

1.1、客户端上传文件提交配置

pom

        <!--  文件提交-->
        <!-- commons-fileupload 包含 commons-io -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>

spring bean xml配置【有时还需要修改tomcat nginx等配置文件】

    <!-- 定义文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880" />
        <property name="maxInMemorySize" value="4096" />
    </bean>

代码开发

    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "2000");
        map.put("size", file.getBytes().length);
        return map;
    } 

服务端接收文件的处理方式如下:推荐使用transferTo 速度比较快

    /**
     * 1、获取 流 方式处理
     *
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadHandler1")
    @ResponseBody
    public Object uploadHandler1(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "2000");
        map.put("size", file.getBytes().length);
        //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
        InputStream is = file.getInputStream();
        return map;
    }

    /**
     * 2、使用file.Transto 来保存上传的文件
     *
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadHandler2")
    @ResponseBody
    public Object uploadHandler2(@RequestParam("file") MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "2000");
        map.put("size", file.getBytes().length);
        File newFile = new File("path");
        //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
        file.transferTo(newFile);
        return map;
    }

    /**
     * 3、使用HttpServletRequest 自己写
     *
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadHandler3")
    @ResponseBody
    public Object uploadHandler3(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "2000");

        //将当前上下文初始化给  CommonsMutipartResolver (多部分解析器)
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)) {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            //获取multiRequest 中所有的文件名
            Iterator iter = multiRequest.getFileNames();

            while (iter.hasNext()) {
                //一次遍历所有文件
                MultipartFile file = multiRequest.getFile(iter.next().toString());
                if (file != null) {
                    String path = "E:/springUpload" + file.getOriginalFilename();
                    //上传
                    file.transferTo(new File(path));
                }
            }
        }
        return map;
    }
View Code

相关文章:

  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2021-08-09
猜你喜欢
  • 2022-01-26
  • 2021-09-17
  • 2022-12-23
  • 2021-11-27
  • 2022-01-06
  • 2022-12-23
  • 2021-10-21
相关资源
相似解决方案