layui实现上传Excel文件,java后台接收并入库

前台代码:

  
$(function(){
    var uploadFileUrl = '../map/upload';
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '../map/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                alert(res);
            }
            ,error: function(){
                //请求异常回调
                alert(111);
            }
            ,accept:'file'//允许的文件类型
            ,auto: false//选择文件后是否自动上传
            ,bindAction:'#submit'

        });
    });
});

后台接收代码:

@RequestMapping(value = "/upload")
@ResponseBody
public Map upload( MultipartFile file, HttpServletRequest request){
    Map modelMap = new HashMap<>();
    List mapPointList = new ArrayList<>();
    //读取IO流文件
    InputStream input = null;
    XSSFWorkbook wb = null;
    try {
        input=file.getInputStream();
        wb=new XSSFWorkbook(input);
        //读取页
        for(int sheetNum=0;sheetNum 
  

需要注意的是,需要在spring-mvc.xml中加如下配置,后台才能正常接收到文件

 

你可能感兴趣的:(java)