cos上传文件(2)


前段时间写的上传文件的例子,并没有考虑到当文件上传时候加入其它信息的情况.

如果要是在页面中加入其它信息像用户名,性别等等..其它信息
要是在处理页面或者servlet中用request进行获得的时候将会
获得时空值,这时因为设置了
ENCTYPE="multipart/form-data 而导致的,至于本质原因应
该和html协议本身时有关的吧!

处理方法cos包中自带了获得方法
multi.getParameter("userName");
看似简单的方法实现起来还真不简单

1. 看了代码发现就是获得的方法不同了

multi.getParameter("userName");
的实现!

  1. if (request.getQueryString() != null) {   
  2.   // Let HttpUtils create a name->String[] structure   
  3.   Hashtable queryParameters =   
  4.     HttpUtils.parseQueryString(request.getQueryString());   
  5.   // For our own use, name it a name->Vector structure   
  6.   Enumeration queryParameterNames = queryParameters.keys();   
  7.   while (queryParameterNames.hasMoreElements()) {   
  8.     Object paramName = queryParameterNames.nextElement();   
  9.     String[] values = (String[])queryParameters.get(paramName);   
  10.     Vector newValues = new Vector();   
  11.     for (int i = 0; i < values.length; i++) {   
  12.       newValues.add(values[i]);   
  13.     }   
  14.     parameters.put(paramName, newValues);   
  15.   }   
  16. }  

 

public String getQueryString()
Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string. Same as the value of the CGI variable QUERY_STRING.

getQueryString 应该似比较原始的方法,request.getparameter(),request.getparameterNames(),
request.getparameterValues()应该都是从他扩展而来的!


parseQueryString

public static Hashtable parseQueryString(String s)
Deprecated. 
Parses a query string passed from the client to the server and builds a HashTable object with key-value pairs. The query string should be in the form of a string packaged by the GET or POST method, that is, it should have key-value pairs in the form key=value, with each pair separated from the next by a & character.

A key can appear more than once in the query string with different values. However, the key appears only once in the hashtable, with its value being an array of strings containing the multiple values sent by the query string.

The keys and values in the hashtable are stored in their decoded form, so any + characters are converted to spaces, and characters sent in hexadecimal notation (like %xx) are converted to ASCII characters.

 

Parameters:
s - a string containing the query to be parsed
Returns:
a HashTable object built from the parsed key-value pairs
Throws:
IllegalArgumentException - if the query string is invalid

2.第一步只是为了将http协议携带的信息进行解析并重新封装,这步是留下的调用接口方法

  1. public String getParameter(String name) {   
  2.   try {   
  3.     Vector values = (Vector)parameters.get(name);   
  4.     if (values == null || values.size() == 0) {   
  5.       return null;   
  6.     }   
  7.     String value = (String)values.elementAt(values.size() - 1);   
  8.     return value;   
  9.   }   
  10.   catch (Exception e) {   
  11.     return null;   
  12.   }   
  13. }  

3.经过测试
直接通过:
String userName1=request.getParameter("userName");
     String userName2=request.getQueryString();
获得的话值是为空的!
只有通过multi.getParameter("userName");才可以!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值