springmvc 上传文件,单个文件,多个文件

1.Java代码部分:

1.1spring配置:

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

1.2Controller层代码(上传单个文件)

@RequestMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        if (!file.isEmpty()) {
            try {
                //文件存放位置
                String path = request.getServletContext().getRealPath("/upload/archive/");
                System.out.println("String path = " + path);
                File dir = new File(path + File.separator + "老人111");
                if (!dir.exists()) {
                    dir.mkdirs();
                }

                // 写文件到服务器
                File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
                file.transferTo(serverFile);
                return "You successfully uploaded file=" + file.getOriginalFilename();
            } catch (Exception e) {
                return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + file.getOriginalFilename() + " because the file was empty.";
        }

    }

1.3Controller层代码(单个input上传多个文件)

@RequestMapping("/uploadManyFile")
    public String uploadManyFile(@RequestParam("file") MultipartFile[] files,//接受参数是数组 HttpServletRequest request) {

        if (files.length != 0) {
            try {
                for (MultipartFile file : files) {
                    //文件存放位置
                    String path = request.getServletContext().getRealPath("/upload/archive/");
                    File dir = new File(path + File.separator + "老人222");
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    // 写文件到服务器
                    File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
                    file.transferTo(serverFile);
                }
                return "成功了";
            } catch (Exception e) {
                return "上传失败了,异常:" + e.getMessage();
            }
        } else {
            return "空的嗷";
        }
    }

1.4Controller层代码(多个input,每个input上传若干个文件)

@RequestMapping("/uploadManyFile")
    public String uploadManyFile(@RequestParam("files1") MultipartFile[] files1,
                                 @RequestParam("files2") MultipartFile[] files2//@RequestParam分别对应input的name
                                 ,HttpServletRequest request) {

        if (files1.length != 0 && files2.length != 0) {
            try {
                for (MultipartFile file : files1) {
                    //文件存放位置
                    String path = request.getServletContext().getRealPath("/upload/archive/");
                    File dir = new File(path + File.separator + "老人333" + File.separator +"身份证");
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    // 写文件到服务器
                    File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
                    file.transferTo(serverFile);
                }
                for (MultipartFile file : files2) {
                    //文件存放位置
                    String path = request.getServletContext().getRealPath("/upload/archive/");
                    File dir = new File(path + File.separator + "老人333" + File.separator +"正明");
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    // 写文件到服务器
                    File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
                    file.transferTo(serverFile);
                }
                return "成功了";
            } catch (Exception e) {
                return "上传失败了,异常:" + e.getMessage();
            }
        } else {
            return "空的嗷";
        }
    }

2.页面部分

2.1上传单个文件页面

<html>
<head>
    <title>Title</title>
</head>
<body>
    <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/home/upload" method="post">
        <input type="file" name="file" >
        <input type="submit" value="提交">
    </form>
</body>
</html>

2.2单个input(type=file)上传多个文件页面

<html>
<head>
    <title>Title</title>
</head>
<body>
    <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/home/uploadManyFile" method="post">
  		 <!-- 一个input上传多个文件,加个multiple-->
        <input type="file" name="file" multiple>
        <input type="submit" value="提交">
    </form>
</body>
</html>

2.3多个input,每个input上传若干个文件页面

<html>
<head>
    <title>Title</title>
</head>
<body>
    <form enctype="multipart/form-data" action="${pageContext.request.contextPath}/home/uploadManyFile" method="post">
        <!-- 每个input的name属性不同-->
        <input type="file" name="files1" multiple>
        <input type="file" name="files2" multiple>
        <input type="submit" value="提交">
    </form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值