编程语言
首页 > 编程语言> > JavaWeb request接收参数3种方式

JavaWeb request接收参数3种方式

作者:互联网

1、form表单提交,encrypt= “application/x-www-form-urlencoded” ,即常规的表单提交 ,  使用POST请求,则请求头中的content-type指定值就是该值

Map<String,String> paramsMap = new HashMap<String,String>() ;
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()) {
	String name = parameterNames.nextElement();
	String value = request.getParameter(name);
	paramsMap.put(name, value) ;
}
log.info("paramsMap :" + paramsMap) ;

 

 

2、post方式提交, Content-Type 为 :  application/json

需要用io流的方式接收参数, 一般为HttpClient请求

StringBuffer buf = new StringBuffer() ;
BufferedReader bfr = new BufferedReader(new InputStreamReader(request.getInputStream())) ;
String line = null ;
while((line = bfr.readLine()) != null) {
	buf.append(line) ;
}
bfr.close();
log.info("params:" + buf.toString()) ;

 

 

3、form表单提交,encrypt= “multipart/form-data” , 其中可以上传文件。 

需要导入apache 的 common.fileupload 相关jar包

request.setCharacterEncoding("UTF-8") ;
DisFileItemFactory factory = new DisFileItemFactory () ;
ServletFileUpload upload = new ServletFileUpload(factory );
upload.setHeaderEncoding("UTF-8") ;
List items = upload.parseRequest(request) ;
Map<String,String> params = new HashMap<String,String>();
for(Object obj : items){
    FileItem fileItem = (FileItem)obj ;
    if(fileItem.isFormField()){
        params.put(fileItem.getFieldName(),fileItem.getString("UTF-8")) ;//页面编码是UTF-8
    }

}
log.info("params:" + params) ;

 

 

 

 

 

标签:JavaWeb,form,paramsMap,request,params,new,接收,fileItem
来源: https://blog.csdn.net/u013792404/article/details/90548167