NutzCN Logo
问答 excel导入上传文件报空指针错误
发布于 2756天前 作者 liumaobudao 3330 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

c

    @AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/assets/upload/tmp" })
    @POST
    @At("/excelimport")
    @Ok("void")
    public void excelimport(@Param("uploadfile") File f){
        try
        {
            log.debug(f.getName());
//            ImportExcel ei = new ImportExcel(f, 1, 0);
//            List<Tea_jxpjbm> list = ei.getDataList(Tea_jxpjbm.class);
         //   log.debug(list.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

f

    <form id="importForm" method="post" enctype="multipart/form-data" action="${base}/platform/jxfzxt/jxpjbm/excelimport"
          style="padding-left:20px;text-align:center;" ><br/>
        <input  name="uploadfile" type="file" style="width:330px"/>导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!<br/>  

    </form>
[DEBUG] 2017-05-07 16:03:49,375 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) - FastUpload : /platform/jxfzxt/jxpjbm/excelimport
[DEBUG] 2017-05-07 16:03:49,378 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) - info created
[DEBUG] 2017-05-07 16:03:49,380 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) - Params map created - 0 params
[DEBUG] 2017-05-07 16:03:49,380 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) - boundary: 
------WebKitFormBoundaryO3qggYkOyeVn7LUN
[DEBUG] 2017-05-07 16:03:49,380 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 2017-05-07 16:03:49,381 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2017-05-07 16:03:49,381 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[],fieldName=[uploadfile]
[DEBUG] 2017-05-07 16:03:49,382 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=,content_type=application/octet-stream
[DEBUG] 2017-05-07 16:03:49,382 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 194 bytes readed
java.lang.NullPointerException
	at cn.wizzer.app.web.modules.controllers.platform.jxfzxt.TeaJxpjbmController.excelimport(TeaJxpjbmController.java:82)
	at cn.wizzer.app.web.modules.controllers.platform.jxfzxt.TeaJxpjbmController$FM$excelimport$b104b5d140fd15da59a2f3e28e8b3209.invoke(TeaJxpjbmController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
15 回复

@wendal 是的 系统报空文件,但是我的excel文件不是空的!

用ajax提交的??? 相关代码贴一下, 页面端的

@wendal
页面的

        <form id="importForm"  enctype="multipart/form-data" action="${base}/platform/jxfzxt/jxpjbm/excelimport" method="post" style="padding-left:20px;text-align:center;" ><br/>
            <input type="file">导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!<br/>  
            <button class="btn btn-primary btn-block btn-lg btn-parsley" data-loading-text="${msg['globals.button.submit.tip']}">${msg['globals.button.submit']}</button>
        </form>
        

action的

    @AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/assets/upload/tmp" })
    @POST
    @At("/excelimport")
    @Ok("json")
    public Object excelimport(File f, HttpServletRequest req, AdaptorErrorContext err){
        try
        {
           // log.debug(uploadfile);MultipartFile
            if (err != null && err.getAdaptorErr() != null) {
                log.debug(f.getName());
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (f == null) {
                log.debug("空文件");
                return Result.error("空文件");
            } else {
                log.debug(f.getName());
                return Result.error("不是空文件");
            }
        //   ImportExcel ei = new ImportExcel(f, 1, 0);
//            List<Tea_jxpjbm> list = ei.getDataList(Tea_jxpjbm.class);
         //   log.debug(list.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统错误");
        }
    }

要加@Param的

页面改成"提交"按钮了? 之前贴的并没有button.

加上@Param,贴现在的日志

@wendal

页面入口
        <a class="btn btn-primary navbar-btn" onclick="_selectAll()"><i class="ti-check"></i>全选</a>
        <button class="btn btn-primary navbar-btn" onclick="excelImport()"><i class="ti-import"></i>导入</button> 
        <a class="btn btn-primary navbar-btn" href="${base}/platform/jxfzxt/jxpjbm/excelexport"><i class="ti-export"></i>导出</a>
        <button class="btn btn-danger navbar-btn" onclick="delCheck()"><i class="ti-close"></i> ${msg['globals.button.delete.more']}</button>
导出js

function excelImport() {
top.layer.open({
type: 1,
area: [500, 300],
title:"导入数据",
content:$("#importBox").html() ,
btn: ['下载模板','确定', '关闭'],
btn1: function(index, layero){
window.location.href='${base}/platform/jxfzxt/jxpjbm/template';
},
btn2: function(index, layero){
var inputForm =top.$("#importForm");
top.$("#importForm").submit();
top.layer.close(index);
},

        btn3: function(index){
            top.layer.close(index);
        }
    });
}
importForm内容
action

@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/assets/upload/tmp" })
@POST
@At("/excelimport")
@Ok("json")
public Object excelimport(@Param("excelfile") File f, HttpServletRequest req, AdaptorErrorContext err){
try
{
// log.debug(uploadfile);MultipartFile
if (err != null && err.getAdaptorErr() != null) {
log.debug(f.getName());
return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
} else if (f == null) {
log.debug("空文件");
return Result.error("空文件");
} else {
log.debug(f.getName());
return Result.error("不是空文件");
}
// ImportExcel ei = new ImportExcel(f, 1, 0);
// List list = ei.getDataList(Tea_jxpjbm.class);
// log.debug(list.toString());
} catch (Exception e) {
e.printStackTrace();
return Result.error("系统错误");
}
}

日志:

[DEBUG] 2017-05-07 18:28:26,269 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [POST] path=/platform/jxfzxt/jxpjbm/excelimport : TeaJxpjbmController.excelimport(TeaJxpjbmController.java:85)
[DEBUG] 2017-05-07 18:28:26,269 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
[DEBUG] 2017-05-07 18:28:26,270 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'sysDictServiceImpl'<class cn.wizzer.app.sys.modules.services.impl.SysDictServiceImpl>
[DEBUG] 2017-05-07 18:28:26,270 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dateUtil'<class cn.wizzer.framework.util.DateUtil>
[DEBUG] 2017-05-07 18:28:26,271 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'stringUtil'<class cn.wizzer.framework.util.StringUtil>
[DEBUG] 2017-05-07 18:28:26,271 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'teaJxpjbmController'<class cn.wizzer.app.web.modules.controllers.platform.jxfzxt.TeaJxpjbmController>
[DEBUG] 2017-05-07 18:28:26,272 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:184) - Select Html4 Form upload parser --> /platform/jxfzxt/jxpjbm/excelimport
[DEBUG] 2017-05-07 18:28:26,272 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) - FastUpload : /platform/jxfzxt/jxpjbm/excelimport
[DEBUG] 2017-05-07 18:28:26,273 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) - info created
[DEBUG] 2017-05-07 18:28:26,273 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) - Params map created - 0 params
[DEBUG] 2017-05-07 18:28:26,274 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) - boundary:
------WebKitFormBoundaryf8pdOhdRPIV0FAEU
[DEBUG] 2017-05-07 18:28:26,274 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 2017-05-07 18:28:26,274 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2017-05-07 18:28:26,275 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[],fieldName=[excelfile] [DEBUG] 2017-05-07 18:28:26,275 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=,content_type=application/octet-stream
[DEBUG] 2017-05-07 18:28:26,276 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 193 bytes readed
[DEBUG] 2017-05-07 18:28:26,276 cn.wizzer.app.web.modules.controllers.platform.jxfzxt.TeaJxpjbmController.excelimport(TeaJxpjbmController.java:89) - 空文件
[DEBUG] 2017-05-07 18:28:26,277 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/platform/jxfzxt/jxpjbm/excelimport 8ms
[DEBUG] 2017-05-07 18:28:43,876 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.

```

写错字了
导入js

function excelImport() {
        top.layer.open({
            type: 1,
            area: [500, 300],
            title:"导入数据",
            content:$("#importBox").html() ,
            btn: ['下载模板','确定', '关闭'],
            btn1: function(index, layero){
                window.location.href='${base}/platform/jxfzxt/jxpjbm/template';
            },
            btn2: function(index, layero){
                var inputForm =top.$("#importForm");
                top.$("#importForm").submit();
                top.layer.close(index);
            },

            btn3: function(index){
                top.layer.close(index);
            }
        });
    }

importForm内容

<div id="importBox" aria-hidden="true">
    <form id="importForm"  enctype="multipart/form-data" action="${base}/platform/jxfzxt/jxpjbm/excelimport" method="post" style="padding-left:20px;text-align:center;" ><br/>
        <!--onsubmit="loading('正在导入,请稍等...');"-->
        <input type="file" name="excelfile">导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!<br/>  
    </form>
</div>

action

 @AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/assets/upload/tmp" })
    @POST
    @At("/excelimport")
    @Ok("json")
    public Object excelimport(@Param("excelfile") File f, HttpServletRequest req, AdaptorErrorContext err){
        try
        {
           // log.debug(uploadfile);MultipartFile
            if (err != null && err.getAdaptorErr() != null) {
                log.debug(f.getName());
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (f == null) {
                log.debug("空文件");
                return Result.error("空文件");
            } else {
                log.debug(f.getName());
                return Result.error("不是空文件");
            }
        //   ImportExcel ei = new ImportExcel(f, 1, 0);
//            List<Tea_jxpjbm> list = ei.getDataList(Tea_jxpjbm.class);
         //   log.debug(list.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统错误");
        }
    }

日志:

[DEBUG] 2017-05-07 18:28:23,855 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.
[DEBUG] 2017-05-07 18:28:26,269 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [POST] path=/platform/jxfzxt/jxpjbm/excelimport : TeaJxpjbmController.excelimport(TeaJxpjbmController.java:85)
[DEBUG] 2017-05-07 18:28:26,269 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
[DEBUG] 2017-05-07 18:28:26,270 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'sysDictServiceImpl'<class cn.wizzer.app.sys.modules.services.impl.SysDictServiceImpl>
[DEBUG] 2017-05-07 18:28:26,270 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dateUtil'<class cn.wizzer.framework.util.DateUtil>
[DEBUG] 2017-05-07 18:28:26,271 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'stringUtil'<class cn.wizzer.framework.util.StringUtil>
[DEBUG] 2017-05-07 18:28:26,271 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'teaJxpjbmController'<class cn.wizzer.app.web.modules.controllers.platform.jxfzxt.TeaJxpjbmController>
[DEBUG] 2017-05-07 18:28:26,272 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:184) - Select Html4 Form upload parser --> /platform/jxfzxt/jxpjbm/excelimport
[DEBUG] 2017-05-07 18:28:26,272 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) - FastUpload : /platform/jxfzxt/jxpjbm/excelimport
[DEBUG] 2017-05-07 18:28:26,273 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) - info created
[DEBUG] 2017-05-07 18:28:26,273 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) - Params map created - 0 params
[DEBUG] 2017-05-07 18:28:26,274 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) - boundary: 
------WebKitFormBoundaryf8pdOhdRPIV0FAEU
[DEBUG] 2017-05-07 18:28:26,274 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 2017-05-07 18:28:26,274 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2017-05-07 18:28:26,275 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[],fieldName=[excelfile]
[DEBUG] 2017-05-07 18:28:26,275 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=,content_type=application/octet-stream
[DEBUG] 2017-05-07 18:28:26,276 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 193 bytes readed
[DEBUG] 2017-05-07 18:28:26,276 cn.wizzer.app.web.modules.controllers.platform.jxfzxt.TeaJxpjbmController.excelimport(TeaJxpjbmController.java:89) - 空文件
[DEBUG] 2017-05-07 18:28:26,277 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/platform/jxfzxt/jxpjbm/excelimport 8ms

问题在于前端代码

你提交的是弹层里面的空表单

@wendal var inputForm =top.$("#importForm");
top.$("#importForm").submit();
这个是空的?

是的,打开chrome的控制台,看看网络请求

@wendal 可是我不掉这个submit
添加按钮 也是空的

importBox里面放个button,直接提交,不走js

@wendal [DEBUG] 2017-05-07 18:54:49,466 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2017-05-07 18:54:49,467 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[123.xlsx],fieldName=[excelfile] [DEBUG] 2017-05-07 18:54:49,467 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=123.xlsx,content_type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
[DEBUG] 2017-05-07 18:54:49,515 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 17418 bytes readed
读出来了 谢谢 兽总!

好(✪▽✪)

页面js修改

    function excelImport() {
        top.layer.open({
            type: 1,
            area: [500, 300],
            title:"导入数据",
            content:"<form id='importForm'  enctype='multipart/form-data' action='${base}/platform/jxfzxt/jxpjbm/excelimport' method='post' style='padding-left:20px;text-align:center;' ><br/> <input type='file' name='excelfile'>导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!<br/></form>",
            btn: ['下载模板','确定', '关闭'],
            btn1: function(index, layero){
                window.location.href='${base}/platform/jxfzxt/jxpjbm/template';
            },
            btn2: function(index, layero){
                $("#importForm").submit();
                top.layer.close(index);
            },
            btn3: function(index){
                top.layer.close(index);
            }
        });
    }
添加回复
请先登陆
回到顶部