NutzCN Logo
问答 上传图片时控制台输出ClassCastException
发布于 2369天前 作者 Tomcat 1697 次浏览 复制 上一个帖子 下一个帖子
标签:

看异常消息,没进入控制器方法。源码:

    @At("/business/draft/?") @POST
    @AdaptBy(type = UploadAdaptor.class, args = "${app.root}/WEB-INF/tmp/draft")
    @Ok("json") @Fail("json")
    public Object uploadLicense(@Param("file") TempFile f, String type, ServletContext ctx) throws IOException {
        if (f == null) Lang.makeThrow("未上传图片");
        String mimeType = f.getContentType(), originName = f.getSubmittedFileName();
        if (!mimeType.startsWith("image/")) {
            f.delete();
            Lang.makeThrow("未知图片类型。");
        }
        if (!Lang.contains(draftImageTypes, type)) {
            f.delete();
            Lang.makeThrow("未知图片属性。");
        }
        return saveToPermanentPath(originName, f.getFile(), ctx, "license");
    }

控制台输出:

WARN  18:58:44.039 o.n.mvc.impl.processor.FailProcessor [http-nio-8080-exec-2] - Error@/business/draft/license :
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
	at org.nutz.mvc.upload.injector.AbstractUploadInjector.getTempFile(AbstractUploadInjector.java:21)
	at org.nutz.mvc.upload.injector.TempFileInjector.get(TempFileInjector.java:16)
	at org.nutz.mvc.upload.injector.TempFileInjector.get(TempFileInjector.java:9)
	at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:294)
	at org.nutz.mvc.upload.UploadAdaptor.adapt(UploadAdaptor.java:120)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.integration.shiro.NutShiroProcessor.process(NutShiroProcessor.java:126)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at nvc.web.IocToElProcessor.process(IocToElProcessor.java:26)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at nvc.web.ShiroFilterImpl.doFilterInternal(ShiroFilterImpl.java:40)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at nvc.web.NutFilterDelegate.doFilter(NutFilterDelegate.java:32)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)


8 回复

nutz版本很老吧?

nutz-1.r.66
用的上传工具,是blueimp的jquery file upload,9.21.0版本。代码:

<input type="file" name="file" v-bind:data-url="url" ref="fileInput">
            var fu = $(me.$refs.fileInput).fileupload({
                dataType: 'json',
                progressall: function(e, data) {
                    me.progressOn(parseInt(data.loaded / data.total * 100, 10));
                },
                done: function(e, data) {
                    me.progressOn();
                    var all = data.result;
                    me.model.splice(0, me.model.length);
                    for (var i = 0; i < all.length; i ++) {
                        me.model.push(all[i]);
                    }
                    if (typeof me.onSuccess == 'function') {
                        me.onSuccess();
                    }
                }
            });

报错前的解析日志发一下

如下:

DEBUG 19:09:37.905 org.nutz.mvc.impl.UrlMappingImpl [http-nio-8080-exec-8] - Found mapping for [POST] path=/business/draft/license : Profile.uploadLicense(Profile.java:87)
DEBUG 19:09:37.905 org.nutz.ioc.impl.NutIoc [http-nio-8080-exec-8] - Get 'iocToElBeans'<>
DEBUG 19:09:37.906 org.nutz.ioc.impl.NutIoc [http-nio-8080-exec-8] - Get 'profile'<class nvc.modules.hbir.ent.Profile>
DEBUG 19:09:37.906 org.nutz.mvc.upload.UploadAdaptor [http-nio-8080-exec-8] - Select Html4 Form upload parser --> /hbir//business/draft/license
DEBUG 19:09:37.921 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - FastUpload : /business/draft/license
DEBUG 19:09:37.924 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - info created
DEBUG 19:09:37.924 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - Params map created - 0 params
DEBUG 19:09:37.928 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - boundary: 
-----------------------------161434797310379860131421255910
DEBUG 19:09:37.939 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - skip first boundary
DEBUG 19:09:37.939 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - Reading...
DEBUG 19:09:37.940 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - Upload File info: FilePath=[db-a.ico],fieldName=[file]
DEBUG 19:09:37.941 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - Upload Info: name=db-a.ico,content_type=image/x-icon
DEBUG 19:09:37.953 org.nutz.mvc.upload.FastUploading [http-nio-8080-exec-8] - ...Done 17180 bytes readed
WARN  19:09:37.957 o.n.mvc.impl.processor.FailProcessor [http-nio-8080-exec-8] - Error@/business/draft/license :
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
	at org.nutz.mvc.upload.injector.AbstractUploadInjector.getTempFile(AbstractUploadInjector.java:21)
	at org.nutz.mvc.upload.injector.TempFileInjector.get(TempFileInjector.java:16)

debug一下AbstractUploadInjector的21行, 看看refer是什么东西

是字符串“license”,看起来像是路径上传来的参数:

@At("/business/draft/?")

调用的时候的这个“?”。

把String type放到@Param("file") TempFile f的前面就好了。

... 原来如此

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