NutzCN Logo
问答 文件上传 在tomcat8下可以上传,tomcat7 request无法传递参数
发布于 2458天前 作者 FWC1994 2416 次浏览 复制 上一个帖子 下一个帖子
标签:

前端文件及相关信息上传代码

var formdata=new FormData();
        		formdata.append("file",file);
        		formdata.append("buildid",this.buildingData.attributes.objectid);
        		formdata.append("filetype",fileType);
        		formdata.append("filename",file.name);
        		var req = new XMLHttpRequest();
        		req.open("POST", "../client/service/comPetingDataController/upLoadFiles", true);
                req.send(formdata);
                req.onload = lang.hitch(this,function(result){
        			console.log(result);
        		})

后台数据接收代码如下

@At("/upLoadFiles")
	@GET
	@POST
	@Ok("json")
	@AdaptBy(type = VoidAdaptor.class)  
	public String upLoadFiles(HttpServletRequest request, HttpServletResponse response,@Param("filename") String filename){
		System.out.println(filename);
		JSONObject res = new JSONObject();
		try {
			Integer buildid = comPetingDataService.upLoadFiles(request);
			res.put("success", true);
			res.put("message", "成功上传图片信息");
			res.put("buildid", buildid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			res.put("success", false);
			res.put("message", e.toString());	
		}
		return res.toString();
		
	}

这里面通过@Param("filename") String filename 或request.getParameter("filename")都无法获得参数
但是如果服务器是tomcat8就没问题,不知道是nutz的原因还是tomcat版本的问题

12 回复

贴后台的解析日志

2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-8] Search mapping for path=/Competing/proxy : NOT Action match
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Found mapping for [POST] path=/client/service/comPetingDataController/upLoadFiles : ComPetingDataController.upLoadFiles(...)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Found mapping for [POST] path=/client/service/comPetingDataController/upLoadFiles : ComPetingDataController.upLoadFiles(...)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2] Found mapping for [POST] path=/client/service/comPetingDataController/upLoadFiles : ComPetingDataController.upLoadFiles(...)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Get 'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] >> Load definition
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2] Get 'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Found IocObject(comPetingDataController) in IocLoader(AnnotationIocLoader@1573582354)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Get 'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] >> Make...'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] class gisuni.lib.competing.controller.ComPetingDataController , no config to enable AOP.
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Get 'comPetingDataService'
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] >> Load definition
null
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Found IocObject(comPetingDataController) in IocLoader(AnnotationIocLoader@1573582354)

D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\seedlandApp\files

2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] >> Make...'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>

0

2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9null
null
] null
class gisuni.lib.competing.controller.ComPetingDataController , no config to enable AOP.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\seedlandApp\files\
1------------------------------
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Get 'comPetingDataService'
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2] >> Load definition
null

D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\seedlandApp\files

0

2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2null

后面自己输出了一些 过程性的值,获得的参数都是null

回复按钮旁边就是 "插入代码或日志片段" 的按钮, 重新发一次!

2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-8] Search mapping for path=/Competing/proxy : NOT Action match
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Found mapping for [POST] path=/client/service/comPetingDataController/upLoadFiles : ComPetingDataController.upLoadFiles(...)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Found mapping for [POST] path=/client/service/comPetingDataController/upLoadFiles : ComPetingDataController.upLoadFiles(...)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2] Found mapping for [POST] path=/client/service/comPetingDataController/upLoadFiles : ComPetingDataController.upLoadFiles(...)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Get 'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] 	 >> Load definition
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2] Get 'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Found IocObject(comPetingDataController) in IocLoader(AnnotationIocLoader@1573582354)
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Get 'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] 	 >> Make...'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] class gisuni.lib.competing.controller.ComPetingDataController , no config to enable AOP.
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-10] Get 'comPetingDataService'<null>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] 	 >> Load definition
null
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Found IocObject(comPetingDataController) in IocLoader(AnnotationIocLoader@1573582354)
D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\seedlandApp\files
------------------------------
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] 	 >> Make...'comPetingDataController'<class gisuni.lib.competing.controller.ComPetingDataController>
0
===============================
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9null
null
] null
class gisuni.lib.competing.controller.ComPetingDataController , no config to enable AOP.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\seedlandApp\files\
1------------------------------
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-9] Get 'comPetingDataService'<null>
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2] 	 >> Load definition
null
D:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\seedlandApp\files
------------------------------
0
===============================
2017-08-02 16:33:49 DEBUG [http-bio-8080-exec-2null

。。。 你用到是VoidAdaptor,它可是什么都不做的

我之前什么适配器都没有用 这块还是在后面通过这段代码 获取参数

public Integer upLoadFiles(HttpServletRequest request) throws Exception{
		request.setCharacterEncoding("utf-8");
		String fileName = "";
		String fileType = "";
		String filePath = "";
		Integer buildId = 0;
		Part file = null;
		//服务器保存路径
		String savePath = request.getServletContext().getRealPath("/files");
		System.out.println(savePath);
		System.out.println("------------------------------");
		//文件在实际工作中不会放在数据库中,数据库永远之存储文件路径
		Collection<Part> parts = request.getParts();
		parts.size();
		System.out.println(parts.size());
		System.out.println("===============================");
		for(Part part:parts){
			if(part.getName().toString().equals("filename")){
				fileName = getStrFromInputSteam(part.getInputStream());
			}else if(part.getName().toString().equals("filetype")){
				fileType = getStrFromInputSteam(part.getInputStream());
			}else if(part.getName().toString().equals("buildid")){
				buildId = Integer.parseInt(getStrFromInputSteam(part.getInputStream()));
			}else{
				file = part;
			}			
		}
		filePath = savePath+"\\"+fileName;
		file.write(filePath);
		System.out.println(request.getParameter("filename"));
		System.out.println(request.getParameter("filetype"));
		System.out.println(request.getParameter("buildid"));
		buildingDataDao.saveFileData(filePath,buildId,fileType);
		return buildId;
	}

但是比较奇怪 tomcat8就正常 tomcat7就一直拿不到参数

Collection<Part> parts = request.getParts();

Filter默认不解析parts,需要设置一下,问谷歌吧

@MultipartConfig nutz支持在controller添加这个注解吗

额, 你继承一下试试吧

这样写对吗 还是获取不到参数

@MultipartConfig
@IocBean(singleton = false)
@At("/client/service/comPetingDataController")
public class ComPetingDataController  extends HttpServlet {

我是说继承NutFilter ,然后在web.xml配自己的Filter类

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