NutzCN Logo
问答 UploadException: Unknow Content-Type : application/x-www-form-urlencoded; charset=UTF-8
发布于 2033天前 作者 Hamming 2938 次浏览 复制 上一个帖子 下一个帖子
标签:

表单提交 有文件上传 代码如下

  <form class="form-horizontal m" id="form-article-edit"  data-parsley-validate enctype="multipart/form-data">
        <input id="id" name="id" th:value="${article.id}" type="hidden">
...
        <div class="form-group">
            <label class="col-sm-3 control-label">文章图片:</label>
            <div class="col-sm-8">
                <input id="img" name="img" th:value="${article.image}" type="file" class="form-control file"  multiple>
            </div>
        </div>

ajax提交表单


function submitHandler() { if ($.validate.form()) { $.operate.save(prefix + "/editDo", $('#form-article-edit').serialize()); } }
            // 保存信息
            save: function(url, data) {
            	$.modal.loading("正在处理中,请稍后...");
            	var config = {
        	        url: url,
        	        type: "post",
        	        dataType: "json",
        	        data: data,
        	        success: function(result) {
        	        	$.operate.successCallback(result);
        	        }
        	    };
        	    $.ajax(config)
            },

服务端接收参数

	/**
	 * 修改保存文章
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("cms:article:edit")
	@Slog(tag="文章", after="修改保存文章")
	@AdaptBy(type = UploadAdaptor.class)
	public Object editDo(@Param("..") Article article, @Param("img") TempFile avatarfile,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(article)){
				article.setUpdateBy(ShiroUtils.getSysUserId());
				article.setUpdateTime(new Date());
				articleService.update(article);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

异常信息

[DEBUG] 17:11:37.026 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) - Get 'articleController'<class io.nutz.nutzsite.module.cms.controller.ArticleController>
[WARN ] 17:11:37.028 org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:197) - Using form upload ? You forgot this --> enctype='multipart/form-data' ?
[WARN ] 17:11:37.034 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/cms/article/editDo :
java.lang.RuntimeException: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/x-www-form-urlencoded; charset=UTF-8
	at org.nutz.lang.Lang.wrapThrow(Lang.java:191)
	at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:202)
	at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:62)
	at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:254)
	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 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.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
	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:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:171)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
	at io.nutz.nutzsite.common.starter.XssFilterStarter.doFilter(XssFilterStarter.java:102)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1602)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:214)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:540)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1701)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1668)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:502)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:370)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:267)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
	at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/x-www-form-urlencoded; charset=UTF-8
	at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:199)
	... 64 more

这个是为什么呢 大神指点

7 回复

换成 @AdaptBy(type = WhaleAdaptor.class) 不报错 但是 接受不到图片

总有人想用ajax上传文件。。。

用ajaxfileupload,webupload等前端插件

呃 应该是这样
直接用jquery的ajax是不能上传文件的.
这时候, 你需要 jquery ajax upload, webupload等第三方js文件上传插件, 它们会通过iframe/flash/html5 file api等技术来实现ajax文件上传.
PS: 据说最新版的jquery支持了走html5 file api进行上传,但我没验证过,暂时当它不行, 估计你也不会用那么高版本的jquery.

关键是这个上传怎么解决的?在你那个开源项目里,出现和你一样的错误.

用ajaxfileupload等js插件

@menglinxi 改成用表单提交 或者 用ajaxfileupload等js插件

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