集册 Java实例教程 使用多部分/表单编码的POST-request apache http客户端

使用多部分/表单编码的POST-request apache http客户端

欢马劈雪     最近更新时间:2020-01-02 10:19:05

532
使用多部分/表单编码的POST请求apache http客户端
/*

 * --------------------------------------------------------------------

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 *   http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing,

 * software distributed under the License is distributed on an

 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

 * KIND, either express or implied.  See the License for the

 * specific language governing permissions and limitations

 * under the License.

 * --------------------------------------------------------------------

 *

 * This software consists of voluntary contributions made by many

 * individuals on behalf of the Apache Software Foundation.  For more

 * information on the Apache Software Foundation, please see

 * <http://www.apache.org/>.

 *

 */

package org.apache.http.examples.entity.mime;
/** 
来 自 
NowJava.com - 时代Java
**/


import java.io.File;


import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;


/**

 * Example how to use multipart/form encoded POST request.

 */

public class ClientMultipartFormPost {/* 来 自 nowjava.com*/


    public static void main(String[] args) throws Exception {

        if (args.length != 1) {

            System.out.println("File path not given");

            System.exit(1);

        }

        CloseableHttpClient httpclient = HttpClients.createDefault();

        try {

            HttpPost httppost = new HttpPost("http://localhost:8080"

                    + "/servlets-examples/servlet/RequestInfoExample");


            FileBody bin = new FileBody(new File(args[0]));

            StringBody comment = new StringBody(

                    "A binary file of some kind", ContentType.TEXT_PLAIN);


            HttpEntity reqEntity = MultipartEntityBuilder.create()

                    .addPart("bin", bin).addPart("comment", comment)

                    .build();


            httppost.setEntity(reqEntity);


            System.out.println("executing request "

                    + httppost.getRequestLine());

            CloseableHttpResponse response = httpclient.execute(httppost);

            try {

                System.out

                        .p
展开阅读全文