在使用httpclient请求用MultipartEntityBuilder配置参数时,远程服务器无法获取参数

在使用httpclient请求时,远程服务器无法获取参数。

1、包版本:httpclient-4.5.3.jar,httpcore-4.4.6.jar,httpmime-4.5.3.jar

2、本地同一服务器通过httpclient调用可以获取到传递的参数(**通过**)

3、本地不同服务器通过httpclient调用可以获取到传递的参数(**通过**)

4、远程服务器通过httpclient调用不能获取到传递的参数(**不通过**)

5、已确保参数名一致,因为换一种写法可以获取到参数

代码:

/**
     * 提交数据,返回JSONObject对象
     * 
     * @param url 请求路径
     * @param headerParams 请求头消息参数
     * @param requestParams 请求业务参数
     * 
     * @return
     */
    public static JSONObject httpPostToJson(String url,JSONObject headerParams,JSONObject requestParams) {
        HttpPost httpPost = null;
        JSONObject jsonObject = new JSONObject();
        try {
             
            // 创建HttpClient实例
            HttpClient httpClient = HttpClients.createDefault();
             
            // 创建httpPost实例,设置超时和头消息
            httpPost = createHttpPost(url,headerParams);
             
            //设置httpPost请求参数
            setHttpRequestParams(httpPost, requestParams);
             
            //执行httpPost请求
            jsonObject = executeHttpClient(httpClient, httpPost);
         
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code", ERROR_STATUS_CODE);
            jsonObject.put("msg", "服务器遇到错误,无法完成请求");
            jsonObject.put("data", "{}");
        }finally{
            if(httpPost != null){
                httpPost.abort(); 
            }
        }
 
       /**
     * 创建httpPost请求,设置超时和头消息
     * 
     * @param _url 请求路径
     * @param headerParams 头参数集合
     * @return
     */
    private static HttpPost createHttpPost(String _url, JSONObject headerParams)throws Exception{
         
        // 创建Post方法实例
        HttpPost httpPost = new HttpPost(_url);
         
        // 设置连接超时
        RequestConfig requestConfig = RequestConfig.custom() 
                .setConnectTimeout(CONNECT_TIME_OUT).setConnectionRequestTimeout(CONNECTION_REQUEST_TIME_OUT)
                    .setSocketTimeout(SOCKET_TIME_OUT).build();  
        httpPost.setConfig(requestConfig);  
         
         // 构造消息头
         
        //设置头参数
        if(headerParams != null){
            for(Object key : headerParams.keySet()){
                httpPost.setHeader(key.toString(), headerParams.getString(key.toString()));
            }
        }
         
        return httpPost;
    }
     
       /**
     * 设置请求参数
     * 
     * @param httpPost
     * @param jsonObj
     * @param in
     */
    private static void setHttpRequestParams(HttpPost httpPost,JSONObject jsonObj)throws Exception{
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builderHttpRequestParams(httpPost, builder, jsonObj);
    }
     
    /**
     * 设置请求参数
     * 
     * @param httpPost
     * @param jsonObj
     * @param in
     */
    private static void builderHttpRequestParams(HttpPost httpPost,MultipartEntityBuilder builder,JSONObject jsonObj)throws Exception{
        if(jsonObj != null){
            for(Object key : jsonObj.keySet()){
                builder.addTextBody(key.toString(), jsonObj.getString(key.toString()));
            }
        }
        HttpEntity entity = builder.build();
        httpPost.setEntity(entity);
    }

解决方案(不是所有环境都能用):

public void processUpload(HttpServletRequest request, HttpServletResponse response){
      
    //检测是不是存在上传文件 
    boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
      
    if(isMultipart){ 
      DiskFileItemFactory factory = new DiskFileItemFactory();
       
      // Create a new file upload handler 
      ServletFileUpload upload = new ServletFileUpload(factory); 
        
      List<FileItem> items = null; 
        
      try { 
        // 解析request请求 
        items = upload.parseRequest(request); 
      } catch (FileUploadException e) { 
        e.printStackTrace(); 
      } 
       
      if(items!=null){ 
        //解析表单项目 
        Iterator<FileItem> iter = items.iterator(); 
        while (iter.hasNext()) { 
          FileItem item = iter.next(); 
           
          //如果是普通表单属性 
          if (item.isFormField()) { 
            //相当于input的name属性  <input type="text" name="content"> 
            String name = item.getFieldName();
             
            //input的value属性 
            String value = item.getString();
             
            System.out.println("属性:" + name + " 属性值:" + value); 
          } 
          //如果是上传文件 
          else { 
            //属性名 
            String fieldName = item.getFieldName(); 
             
            //上传文件路径 
            String fileName = item.getName(); 
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);// 获得上传文件的文件名 
             
          
          } 
        } 
      } 
    } 
  }

相关推荐