【转载】SpringMVC:用MultipartFile上传单个文件,多个文件

转自:https://www.baidu.com/link?url=RSk1YWBYyk5vFamWc-SQaGuo6SLCQqaiTpiI3ca60f1-DfxRzSX601FfsTH1XbAOQOY_bUcseWzm_oIXFzSBZM_GnN93iQG9A5lD12vDfii&wd=&eqid=a98c062e00005fc2000000025bff9b54

单个文件上传开发步骤:

1.添加Apache文件上传jar包

首先需要下载两个apache上传文件的jar包 
commons-fileupload-1.3.1.jar 
commons-io-2.4.jar 
具体使用版本,请根据项目进行选择。 

2.配置MultipartResolver处理文件

SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file。

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5400000" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

属性介绍: 
maxUploadSize:最大上传文件的大小,单位为字节; 
defaultEncoding:请求的编码格式,默认为iso-8859-1。

3.编写文件上传控制器

@Controller
public class FileUploadController {

    private static String UPLOAD_DIRECTORY = PropertiesUtil.get("fileupload.directory", "");

    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public ModelAndView uploadFile(@RequestParam("file") MultipartFile file){
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                //判断文件目录是否存在,否则自动生成
                File directory = new File(UPLOAD_DIRECTORY);
                if (!directory.exists()){
                    directory.mkdirs();
                }

                //失败跳转视图
                if (file.getSize() > 30000)
               return new ModelAndView("uploadFail","msg",file.getOriginalFilename()+"超过了指定大小");

                // 文件保存路径
                String filePath =  FilenameUtils.concat(UPLOAD_DIRECTORY, file.getOriginalFilename());
                // 转存文件
                file.transferTo(new File(filePath));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //成功跳转视图
        return new ModelAndView("uploadSuccess","msg",file.getOriginalFilename());
    }

}
 

说明: 
1>使用SpringMVC注解@RequestParam来获取表单中的file参数内容; 
2>通过MultipartFile的transferTo(File dest)这个方法来直接转存文件到指定的路径。

4.书写前段测试表单

upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Upload File</title>
</head>
<body>
    <h2>文件上传实例</h2>
    <form action="/uploadFile" method="post" enctype="multipart/form-data">
        选择文件:<input type="file" name="file">
        <input type="submit" value="提交">
    </form>
</body>
</html>

uploadSuccess.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传成功</title>
</head>
<body>
    <h1>文件${msg}上传成功!</h1>
</body>
</html>

 

5.运行单个文件上传结果

成功: 

上传成功

失败

上传失败

 

多个文件上传开发步骤:

步骤1,2都是相同的,只是步骤3,4有些许不同。

3.1多个文件上传控制器

    /**
     * 多个文件上传
     * @param files
     * @return
     */
    @RequestMapping(value = "uploadFiles", method = RequestMethod.POST)
    public ModelAndView uploadFile(@RequestParam("files") MultipartFile[] files){
        //判断file数组不能为空并且长度大于0
        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];

                //保存文件
                String filePath = FilenameUtils.concat(UPLOAD_DIRECTORY, file.getOriginalFilename());
                // 转存文件
                try {
                    file.transferTo(new File(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //跳转视图
        return new ModelAndView("uploadSuccess","msg",files.length+"个文件");
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
4.1多个文件上传表单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Upload File</title>
</head>
<body>
    <h2>多个文件上传实例</h2>
    <form action="/uploadFiles" method="post" enctype="multipart/form-data">
        <p> 选择文件:<input type="file" name="files">
        <p> 选择文件:<input type="file" name="files">
        <p> 选择文件:<input type="file" name="files">
        <input type="submit" value="提交">
    </form>
</body>
</html>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值