spring:十二、springMVC文件上传(多个文件上传)

maven依赖

<!--        文件上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

springmvc配置文件上传处理器

<!--    文件上传处理器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--        文件最大值-->
        <property name="maxUploadSize" value="10485760000"></property>
<!--        默认编码-->
        <property name="defaultEncoding" value="UTF-8"></property>

    </bean>

文件上传前端页面

upfile.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/6/2
  Time: 10:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传多个文件</title>
</head>
<body>
选择文件1:<input type="file" name="file1" id="file1"><br>
选择文件2:<input type="file" name="file2" id="file2"><br>
选择文件3:<input type="file" name="file3" id="file3"><br>
<input type="submit" value="上传" id="upbtn">

<script type="text/javascript" src="js/1.jq.min.js"></script>
<script type="text/javascript">
    $(function () {

        $("#upbtn").click(function (e) {
            var file1 = $("#file1")[0].files[0];
            var file2 = $("#file2")[0].files[0];
            var file3 = $("#file3")[0].files[0];
            var formData = new FormData;
            formData.append("files",file1);
            formData.append("files",file2);
            formData.append("files",file3);
            $.ajax({
                type:"POST",
                url:"upload",
                async:false,
                contentType:false, // 必须写,因为FormData对象会设置正确的Content-Type
                processData:false,// 必须写,因为FormData对象需要自己来发送数据
                data:formData,
                dataType:'text',
                success: function(response) {
                    // 处理响应
                    alert(response);
                },
                error: function(textStatus, errorThrown) {
                    // 处理错误
                    console.log('Error: ' + textStatus, errorThrown);
                }
            });

        });

    });
</script>


</body>
</html>

文件上传需要把获取到的文件流传入FormData

 contentType:false, // 必须写,因为FormData对象会设置正确的Content-Type
 processData:false,// 必须写,因为FormData对象需要自己来发送数据

controller处理文件上传请求

package com.upload.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UpFileController {
    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile[] files, HttpServletRequest request) throws IOException {
        if (files==null) {
            return "files can not empity";
        }
       else {
           //设置上传文件夹的真实路径
            //String realPath = request.getSession().getServletContext().getRealPath("/images/uploads");
            String realPath="D:\\桌面\\spring学习\\springdemo\\spring_mvc3\\src\\main\\web\\images";
            File dir = new File(realPath);
            if(!dir.exists()){
                dir.mkdirs();
            }

            for (MultipartFile file : files) {
                String filename = file.getOriginalFilename();
                filename = UUID.randomUUID()+"_" +filename;
                //创建空文件
                File newFile = new File(dir,filename);
                file.transferTo(newFile);


            }
            return "sucess";
        }

    }


}

文件上传接收参数

public String upload(MultipartFile[] files, HttpServletRequest request)

多文件上传采用MultipartFile[]数组接受,文件名files必须与前端设置的参数名一致

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值