使用Apache Commons FileUpload组件上传文件

引入依赖:

 <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

使用Apache Commons FileUpload组件上传文件时

List<FileItem> items = upload.parseRequest(request);

items 大小总是为0,在Spring Boot中有默认的文件上传组件,在使用ServletFileUpload时需要关闭Spring Boot的默认配置 

在SpringBoot 2.x中使用Apache Commons FileUpload组件上传时,需要关闭SpringBoot默认上传组件2.x配置如代码块所示。1.x的配置为:spring.http.multipart.enabled=false

#禁用MultipartResolver提供的默认值
spring:
  servlet:
    multipart:
      enabled: false

页面Demo:

表单上传注意事项:form表单的内容格式要定义成multipart/form-data格式,post请求

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta  charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
<form action="fileUpload" method="post" enctype="multipart/form-data">
    <div align="center">
        <fieldset style="width:80%">
            <legend>上传文件</legend><br/>
            <div align="left">上传文件1</div>
            <div align="left">
                <input type="file" name="file1"/>
            </div>
            <div align="left">上传文件2</div>
            <div align="left">
                <input type="file" name="file2"/>
            </div>
            <div>
                <div align='left'>上传文件描述1</div>
                <div align='left'><input type="text" name="description1"/></div>
            </div>
            <div>
                <div align='left'>上传文件描述2</div>
                <div align='left'><input type="text" name="description2"/></div>
            </div>
            <div>
                <div align='left'>
                    <input type='submit' value="上传文件"/>
                </div>
            </div>
        </fieldset>
    </div>
</form>
</div>
</body>
</html>

后台代码工具类:

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class FileUpload {

    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public void fileUpload(HttpServletRequest request) throws Exception {
        boolean upload = uploadFileUtil(request);
        System.out.println("上传结果" + upload);
    }

     //在开发测试模式时,得到的地址为:{项目跟目录}/target/classes/excelupload/
    //在打包成jar正式发布时,得到的地址为:{发布jar包目录}/excelupload
    public static boolean uploadFileUtil(HttpServletRequest request) throws Exception {
        //获取跟目录
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        if (!path.exists()) {
            path = new File("");
        }
        //如果上传目录为根目录下面的excelupload,则可以如下获取
        File upload = new File(path.getAbsolutePath(), "excelupload/");
        if (!upload.exists()) {
            upload.mkdirs();
        }
        System.out.println("upload url:" + upload.getAbsolutePath());
        //拿到请求上传的文件
        Map<String, Object> map = uploadFile(request);
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> mapEntry : entries) {
            Object value = mapEntry.getValue();
            if (value instanceof FileItem) {
                FileItem fileItem = (FileItem) value;
                if(fileItem.getSize()>0) {
                    String key = mapEntry.getKey();
                    String[] split = key.split("\\:");
                    fileItem.write(new File(upload.toString()+"/" + split[1]));
                }
            } else {
                System.out.println("该处为上传文件描述内容:" + mapEntry.getKey()+"值为:"+mapEntry.getValue());
            }
        }
        return true;
    }

    public static Map<String, Object> uploadFile(HttpServletRequest request) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        //1、使用apache commons-fileupload组件文件上传
        // 默认最多允许存储1024个字节
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //超过10M以上的文件 通过临时文件夹来上传 10*1024
        factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
        //判断表单属性enctype,值为“multipart/form-data”
        if (ServletFileUpload.isMultipartContent(request)) {
            //创建一个FileItem工厂 通过DiskFileItemFactory对象创建文件上传核心组件
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(1024 * 1024 * 20);
            upload.setHeaderEncoding("UTF-8");
            try {
                //通过文件上传核心组件解析request请求,获取到所有的FileItem对象
                List<FileItem> items = upload.parseRequest(request);
                //遍历表单的所有表单项(FileItem) 并对其进行相关操作
                for (FileItem item : items) {
                    //判断这个表单项如果是一个普通的表单项
                    if (item.isFormField()) {
                        String fieldName = item.getFieldName();
                        String value = new String(item.getString("UTF-8"));
                        parameters.put(fieldName, value);
                    } else {//如果不是表单的普通文本域,就是文件上传域,取输入流
                        //文件上传域,取输入流,如果是文件流的话,设置key为,form表单+文件名":"分割
                        parameters.put(item.getFieldName() + ":" + item.getName(), item);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return parameters;
    }
}

Ajax请求上传文件:

页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
   <!-- <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>-->
    <script type="text/javascript">
       $(function () {
           $("#uploadBtn").click(function () {
               var formData = new FormData();
               //注意:这里不能获取文件的路径,路径是一个字符串,这里应该是一个“文件上传控件的DOM对象”。
               formData.append("FileUpload", $("#FileUpload")[0].files[0]);
               //发送ajax post请求,向服务器传一个excel文件
               $.ajax({
                   type: "post",
                   url: "ajaxFileUpload",
                   data: formData,
                   beforeSend: function () {
                       return true;
                   },
                   contentType: false, //不设置请求头的内容类型,默认的请求头类型是:application/x-www-form-urlencoded,这里不采用它,用:formData ,对应的是:enctype=multipart/form-data
                   processData: false, //禁止jquery对formData进行格式化预处理
                   success: function (json) {
                       alert(json)
                       if (json.result) {
                           alert("上传成功")
                       } else {
                           alert("上传失败");
                       }
                   }
               });
           });
       });
    </script>
</head>
<body>
<div align="left">上传文件Ajax</div>
<div align="left">
    <input type="file" name="file1" id="FileUpload"/>
</div>
<button type="button" id="uploadBtn">Ajax上传</button>
</div>
</body>
</html>

后端代码: 

    @RequestMapping(value = "/ajaxFileUpload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> ajaxFileUpload(HttpServletRequest request) throws Exception {
        Map<String,Object> map =new HashMap<>();
        boolean upload = uploadFileUtil(request);
        System.out.println("Ajax上传结果" + upload);
        if (upload) {
            map.put("result", upload);
        } else {
            map.put("result", upload);
        }
        return map;
    }

 

 

 

参考:Spring MVC使用commons fileupload实现文件上传功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值