httppost请求传递参数为file的集合

package com.zjxt.demo.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: heiheihaxi
 * @Date: 2019/4/26 10:02
 */
public class TestUploader {
    public static void main(String[] args) throws IOException {
        String url="http://localhost:8085/Uploader/";
        List<File> files = new ArrayList<>();
        File f1=new File("C:\\Users\\zzh\\Desktop\\tmp\\临时.jpg");
        File f2=new File("C:\\Users\\zzh\\Desktop\\tmp\\aaa.jpg");
        files.add(f1);
        files.add(f2);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //设置参数
        MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
        // 解决乱码问题
        meBuilder.setCharset(Charset.forName("UTF-8"));
        meBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        for (File file : files) {
            FileBody fileBody = new FileBody(file , ContentType.create("multipart/form-data",Charset.forName("UTF-8")));
            meBuilder.addPart("files", fileBody);
        }
        HttpEntity reqEntity = meBuilder.build();
        httpPost.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(httpPost);
        if(response != null){
            HttpEntity resEntity = response.getEntity();
            if(resEntity != null){
                String result = EntityUtils.toString(resEntity,"utf-8");
                System.out.println(result);
            }
        }

    }
}

如果参数为普通的字符串参数,可以用以下方式

package com.zjxt.demo.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
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.message.BasicNameValuePair;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: heiheihaxi
 * @Date: 2019/4/26 10:23
 */
public class Test {
    public static void main(String[] args) throws IOException {
        String url="http://localhost:8087/Uploader/";

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);// 创建httpPost

        File f1=new File("C:\\Users\\zzh\\Desktop\\tmp\\bbb.mp3");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("fileName", f1.getName()));
        nvps.add(new BasicNameValuePair("lastModified", Long.toString(f1.lastModified())));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps));

        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state == HttpStatus.SC_OK) {
                HttpEntity responseEntity = response.getEntity();
                System.out.println("responseEntity:"+responseEntity);
            }
            else{
                System.out.println("请求返回:"+state+"("+url+")");
            }
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值