NutzCN Logo
问答 文件上传方法 使用postman 测试 后台报错Unknow Content-Type : application/json
发布于 1451天前 作者 ChenQXin 1728 次浏览 复制 上一个帖子 下一个帖子
标签:

2020-11-30 16:04:45,849 [http-nio-8080-exec-4] WARN org.nutz.mvc.impl.processor.FailProcessor - Error@/shoufa/saves :
java.lang.RuntimeException: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/json
at org.nutz.lang.Lang.wrapThrow(Lang.java:185)
at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:199)
at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:62)
at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:250)
at org.nutz.mvc.upload.UploadAdaptor.adapt(UploadAdaptor.java:117)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:25)
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.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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)
Caused by: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/json
at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:196)
... 35 more
2020-11-30 16:04:45,849 [http-nio-8080-exec-4] WARN org.nutz.mvc.impl.processor.FailProcessor - Error@/shoufa/saves :
java.lang.RuntimeException: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/json
at org.nutz.lang.Lang.wrapThrow(Lang.java:185)
at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:199)
at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:62)
at org.nutz.mvc.adaptor.AbstractAdaptor.adapt(AbstractAdaptor.java:250)
at org.nutz.mvc.upload.UploadAdaptor.adapt(UploadAdaptor.java:117)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:25)
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.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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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)
Caused by: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/json
at org.nutz.mvc.upload.UploadAdaptor.getReferObject(UploadAdaptor.java:196)
... 35 more


@IocBean @At("/shoufa") @Fail("http:500") @Ok("json:{locked:'password|salt'}") @AdaptBy(type = JsonAdaptor.class) @Filters({ @By(type = CrossOriginFilter.class), @By(type = TokenServlet.class) }) public class TShoufaModule { @Inject private TShoufaService tTaskService; @Inject private UsersService usersService; @At @AdaptBy(type = UploadAdaptor.class, args = { "ioc:myPicUpload"}) public Object saves(@Param("img")TempFile [] imgs,HttpServletRequest req,@Param("uid") String uid, @Param("tid")String tid,@Param("tit")String tit,@Param("cons")String cons, @Param("mes")String mes,@Param("tale")int tale,@Param("cluid")String cluid ){ //支线任务添加 //Users users =(Users)session.getAttribute("users"); Decode d =new Decode(); TShoufa t =new TShoufa(); //t.setTask_zid(d.getzhongwen(tid));//主任务id t.setTask_tit(d.getzhongwen(tit));//标题 t.setTask_cons(d.getzhongwen(cons));//内容 t.setTask_level(tale);//等级 t.setTask_staruid(uid);//发起人id t.setTask_cluid(d.getzhongwen(cluid));//处理人id(可多人) t.setTask_dclrid(d.getzhongwen(cluid));//主标添加待处理人(辅助查询) t.setTask_sjclid(uid); String[] user =d.getzhongwen(cluid).split(","); // System.out.println(users); List<String> userssss =Arrays.asList(user); new TuiSong(userssss, d.getzhongwen(cluid)).start(); tTaskService.saves(req, imgs, t); return true; }
8 回复
@Override
	@Aop(TransAop.READ_COMMITTED)
	public void saves(HttpServletRequest req, TempFile[] imgs, TShoufa tasks) {
		StringBuffer names = new StringBuffer();
		StringBuffer imgss = new StringBuffer();
		for (int i = 0; i < imgs.length; i++) {
			FieldMeta meta = imgs[i].getMeta();
			String oldName = meta.getFileLocalName();
			String oldtype = oldName.substring(oldName.lastIndexOf("."));

			File f = imgs[i].getFile();
			StringBuffer imgname = new StringBuffer(new GUID().getguid())
					.append(oldtype);
			if (i > 0) {
				imgss.append(",");
				names.append(",");
			}
			imgss.append(imgname);
			names.append(oldName);
			StringBuffer path = new StringBuffer(req.getServletContext()
					.getRealPath("")).append("/img/").append(imgname);
			File toFile = new File(path.toString());
			InputStream input = null;
			OutputStream out = null;
			try {
				input = new FileInputStream(f);
				out = new FileOutputStream(toFile);
				byte[] buffered = new byte[1024 * 1024 * 2];
				int length = 0;
				while ((length = input.read(buffered)) != -1) {
					out.write(buffered, 0, length);
				}
				input.close();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		String cluidss =tasks.getTask_cluid();
		
		String[] cluids =cluidss.split(",");
		for (String s : cluids) {
			TShoufa task =new TShoufa();
			task =tasks;
			String tid =new GUID().getguid();
			
			if("".equals(imgss.toString())){
				task.setTask_path(null);
			}else {
				task.setTask_path(imgss.toString());
			}
			
			if("".equals(names.toString())){
				task.setTask_yname(null);
			}else {
				task.setTask_yname(names.toString());
			}
			
			Date date =new Date();
			task.setTask_id(tid);
			task.setTask_stime(date);
			task.setTask_state(0);
			task.setTask_dclrid(s);
			task.setTask_sjclid(s);
		//	System.out.println(task.getTask_sjclid());
			
			tTaskDAO.save(task);
			
			
			
		}
		
		
		
	}

文件上传是mulitpart

@wendal 前端 contentType就 设置的multipart/form-data

 config.headers = {
                'Authorization': sessionStorage.getItem('token'),
                'Content-Type': "multipart/form-data;boundary ="+ new Date().getTime()
            };

我现在把module层@AdaptBy(type = UploadAdaptor.class, ) 换成了WhaleAdaptor.class 但是 前端只能传一个文件 传多个文件过来 我后台直接收到不 数据

@At
	 @AdaptBy(type = WhaleAdaptor.class, args = { "ioc:myPicUpload" })
	public Object saves(@Param("img")TempFile [] imgs,HttpServletRequest req,@Param("uid") String uid,
			@Param("tid")String tid,@Param("tit")String tit,@Param("cons")String cons,
			@Param("mes")String mes,@Param("tale")int tale,@Param("cluid")String cluid
			){
		//支线任务添加
		//Users users =(Users)session.getAttribute("users");
		Decode d =new Decode();
		TShoufa t =new TShoufa();
		  //t.setTask_zid(d.getzhongwen(tid));//主任务id
			t.setTask_tit(d.getzhongwen(tit));//标题
			t.setTask_cons(d.getzhongwen(cons));//内容
			t.setTask_level(tale);//等级
			t.setTask_staruid(uid);//发起人id
			t.setTask_cluid(d.getzhongwen(cluid));//处理人id(可多人)
			t.setTask_dclrid(d.getzhongwen(cluid));//主标添加待处理人(辅助查询)
			t.setTask_sjclid(uid);
			String[] user =d.getzhongwen(cluid).split(",");
			//	System.out.println(users);
			List<String> userssss =Arrays.asList(user);	
			new TuiSong(userssss, d.getzhongwen(cluid)).start();
			
			tTaskService.saves(req, imgs, t);
			return true;
		
		
		
	}

@wendal 这是前端 浏览器 显示传递的数据 img的类型 如果直接是一个文件 可以直接上传 如果是个文件数组 我后台接收 全是空数据

studyname: 
1111
studycontent: 
1111
studytype: 
4024335320D84D198D6FC3E29A552E62
img: 
[object File],[object File]
uid: 
BE65BC3D530A4D41AAD749DF5ED65F04

那是你前端代码的锅了

@wendal 为啥 img 上传多文件 会显示 [object File]

@wendal 我让前端 再研究研究吧 感谢!!

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