NutzCN Logo
问答 fileinput插件如何搭配nutz使用
发布于 2671天前 作者 bb3239 1591 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我使用fileinput插件上传文件,请问入口方法该怎么改?
前端配置参数如下:

   var options ={
            language: 'zh', //设置语言
            allowedPreviewTypes : [ 'image' ],
            allowedFileTypes: ['image','text','object','flash'],//默认是['image', 'html', 'text', 'video', 'audio', 'flash','object']
            uploadUrl: "${base}/file/upload/file", //上传的地址
//            allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀,
            maxFileCount: 2000,
            enctype: 'multipart/form-data',
            showUpload: true, //是否显示上传按钮
            showCaption: true,//是否显示标题
            showRemove : true,
            browseClass: "btn btn-primary", //按钮样式             
            previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
    };

入口方法如下:

@AdaptBy(type = UploadAdaptor.class)
    @POST
    @At
    @Ok("json")
    @RequiresAuthentication
    public Object file(@Param("file") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (tf == null) {
                return Result.error("空文件");
            } else {
                String p = Globals.AppRoot;
                String f = Globals.AppUploadPath + "/file/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
                Files.write(new File(p + f), tf.getInputStream());
                return Result.success("上传成功", Globals.AppBase+f);
            }
        } catch (Exception e) {
            return Result.error("系统错误");
        } catch (Throwable e) {
            return Result.error("图片格式错误");
        }
    }
6 回复

看日志里面的表单名

是file_data么?

[DEBUG] 2017-07-24 17:54:43,430 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 2017-07-24 17:54:43,430 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2017-07-24 17:54:43,431 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[三维场景.jpg],fieldName=[file_data]
[DEBUG] 2017-07-24 17:54:43,432 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=三维场景.jpg,content_type=image/jpeg
[DEBUG] 2017-07-24 17:54:43,475 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[null],fieldName=[file_id]
[DEBUG] 2017-07-24 17:54:43,475 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) - Found a param, name=[file_id] value=[0]
[DEBUG] 2017-07-24 17:54:43,476 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 180628 bytes readed
[DEBUG] 2017-07-24 17:55:55,997 org.quartz.impl.jdbcjobstore.JobStoreSupport$MisfireHandler.manage(JobStoreSupport.java:3949) - MisfireHandler: scanning for misfires...
[DEBUG] 2017-07-24 17:55:56,005 org.quartz.impl.jdbcjobstore.JobStoreSupport.doRecoverMisfires(JobStoreSupport.java:3198) - Found 0 triggers that missed their scheduled fire-time.
[DEBUG] 2017-07-24 17:56:56,717 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.
[DEBUG] 2017-07-24 17:57:10,826 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/file/upload/file 147434ms
[DEBUG] 2017-07-24 17:57:14,091 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.
[DEBUG] 2017-07-24 17:57:34,069 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.
[DEBUG] 2017-07-24 17:57:48,726 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [POST] path=/file/upload/file : UploadfileController.file(UploadfileController.java:62)
[DEBUG] 2017-07-24 17:57:48,726 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
[DEBUG] 2017-07-24 17:57:48,726 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dateUtil'<class cn.wizzer.framework.util.DateUtil>
[DEBUG] 2017-07-24 17:57:48,726 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'stringUtil'<class cn.wizzer.framework.util.StringUtil>
[DEBUG] 2017-07-24 17:57:48,727 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'uploadfileController'<class cn.wizzer.app.tunnel.modules.controllers.file.UploadfileController>
[DEBUG] 2017-07-24 17:57:48,727 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:184) - Select Html4 Form upload parser --> /file/upload/file
[DEBUG] 2017-07-24 17:57:48,727 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) - FastUpload : /file/upload/file
[DEBUG] 2017-07-24 17:57:48,728 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) - info created
[DEBUG] 2017-07-24 17:57:48,728 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) - Params map created - 0 params
[DEBUG] 2017-07-24 17:57:48,728 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) - boundary: 

经过UploadAdaptor适配后,似乎原有文件名就没了,我想保留原有文件名,咋办?

找到了getSubmittedFileName,谢谢

@wendal 后台如何接收多文件上传呢?适配器适配完之后始终都是一个,代码如下:

    @AdaptBy(type = UploadAdaptor.class)
    @POST
    @At
    @Ok("json")
    @RequiresAuthentication
    public Object file(@Param("file_data")TempFile[] tfs, HttpServletRequest req, AdaptorErrorContext err) {
        try {
            if (err != null && err.getAdaptorErr() != null) {
                return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
            } else if (tfs == null) {
                return Result.error("空文件");
            } else {

看解析日志

添加回复
请先登陆
回到顶部