HttpClient以multipart/form-data上传文件

 

HttpClient以multipart/form-data上传文件

 

 

    public static String doPostUpload(String url, Map<String, ContentBody> mapParam, String headers) {

        // 创建请求连接,添加Cookie
        CloseableHttpClient closeableHttpClient = null;
        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie( "JSESSIONID", JSESSIONID );
        cookie.setVersion( Version );
        cookie.setDomain( Domain );
        cookie.setPath( Path );
        cookieStore.addCookie( cookie );

        closeableHttpClient = HttpClients.custom()
                .setProxy( proxy )
                .setRedirectStrategy( new LaxRedirectStrategy() ) //自动跟随重定向
                .setConnectionTimeToLive( 6000, TimeUnit.MILLISECONDS )
                .setDefaultCookieStore( cookieStore )
                .build();

        HttpPost httpPost = new HttpPost( url );
        System.out.println( "请求地址 " + httpPost.getURI() );

        //setConnectTimeout:设置连接超时时间,单位毫秒。
        // setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
        // setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
        RequestConfig defaultRequestConfig = RequestConfig.custom().
                setConnectTimeout( 5000 )
                .setConnectionRequestTimeout( 5000 )
                .setSocketTimeout( 15000 )
                .build();
        httpPost.setConfig( defaultRequestConfig );

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

        for (Map.Entry<String, ContentBody> param : mapParam.entrySet()) {
            multipartEntityBuilder.addPart( param.getKey(), param.getValue() );
        }
        //multipartEntityBuilder.setCharset( Charset.forName( "utf-8" ));
        multipartEntityBuilder.setMode( HttpMultipartMode.BROWSER_COMPATIBLE ); //加上此行代码解决返回中文乱码问题

        HttpEntity reqEntity = multipartEntityBuilder.build();
        httpPost.setEntity( reqEntity );

        // 处理头部信息
        if (!CommonMethord.isEmpty( headers )) {
            String[] header_array = headers.split( ";" );
            for (String line : header_array) {
                String[] header = line.split( "=" );
                httpPost.addHeader( header[0], header[1] );
            }
        }
        // 获取返回对象
        String result = ""; //
        CloseableHttpResponse resp = null; //返回结果

        try {
            // 发送请求
            resp = closeableHttpClient.execute( httpPost );

            // http 状态 200 404 302 500
            StatusLine line = resp.getStatusLine();
            System.out.println( "返回响应码: " + line );

            // 结果
            HttpEntity httpEntity = resp.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString( httpEntity, Charset.forName( "UTF-8" ) );
                System.out.println( "返回结果: " + result );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭连接
                closeableHttpClient.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

  

 

//  测试代码

HttpClient以multipart/form-data上传文件

 

上一篇:springboot 简单测试文件上传


下一篇:Cannot retrieve definition for form bean null on action错误