Search in sources :

Example 1 with MultipartEntityBuilder

use of org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder in project weicoder by wdcode.

the class HttpUpload method upload.

/**
 * 上传文件
 *
 * @param  url  post提交地址
 * @param  data 提交参数
 * @param  name 参数名
 * @param  b    流
 * @return      返回结果
 */
public static String upload(String url, Map<String, Object> data, String name, byte[] b) {
    // 如果文件为空
    if (U.E.isEmpty(url) || U.E.isEmpty(b))
        return StringConstants.EMPTY;
    // 声明HttpPost
    HttpPost post = null;
    try {
        // 获得HttpPost
        post = new HttpPost(url);
        post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, "multipart/form-data"));
        // 多提交实体构造器
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 设置浏览器上传
        builder.setMode(HttpMultipartMode.EXTENDED);
        // 添加上传文件
        builder.addBinaryBody(name, b);
        // 参数
        if (U.E.isNotEmpty(data))
            // 设置参数
            data.forEach((k, v) -> builder.addTextBody(k, W.C.toString(v)));
        // 设置提交文件参数
        post.setEntity(builder.build());
        // 返回结果
        return IOUtil.readString(HttpClient.CLIENT.execute(post).getEntity().getContent());
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        // 销毁post
        if (post != null)
            post.abort();
    }
    return StringConstants.EMPTY;
}
Also used : UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) Lists(com.weicoder.common.lang.Lists) Log(com.weicoder.common.log.Log) IOUtil(com.weicoder.common.io.IOUtil) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) W(com.weicoder.common.W) File(java.io.File) U(com.weicoder.common.U) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) List(java.util.List) StringConstants(com.weicoder.common.constants.StringConstants) HttpConstants(com.weicoder.common.constants.HttpConstants) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Map(java.util.Map) LogFactory(com.weicoder.common.log.LogFactory) HttpMultipartMode(org.apache.hc.client5.http.entity.mime.HttpMultipartMode) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Example 2 with MultipartEntityBuilder

use of org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder in project weicoder by wdcode.

the class HttpUpload method upload.

/**
 * 上传文件
 *
 * @param  url   post提交地址
 * @param  data  提交参数
 * @param  files 上传文件
 * @return       返回结果
 */
public static String upload(String url, Map<String, Object> data, File... files) {
    // 如果文件为空
    if (U.E.isEmpty(url) || U.E.isEmpty(files))
        return StringConstants.EMPTY;
    // 声明HttpPost
    HttpPost post = null;
    try {
        // 获得HttpPost
        post = new HttpPost(url);
        // 参数
        if (U.E.isNotEmpty(data)) {
            // 声明参数列表
            List<NameValuePair> list = Lists.newList(data.size());
            // 设置参数
            data.forEach((k, v) -> list.add(new BasicNameValuePair(k, W.C.toString(v))));
            // 设置参数与 编码格式
            post.setEntity(new UrlEncodedFormEntity(list));
        }
        // 多提交实体构造器
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 设置浏览器上传
        builder.setMode(HttpMultipartMode.EXTENDED);
        // 添加上传文件
        for (File file : files) builder.addBinaryBody(file.getName(), file);
        // 设置提交文件参数
        post.setEntity(builder.build());
        // 返回结果
        return IOUtil.readString(HttpClient.CLIENT.execute(post).getEntity().getContent());
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        // 销毁post
        if (post != null) {
            post.abort();
        }
    }
    return StringConstants.EMPTY;
}
Also used : HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) File(java.io.File)

Aggregations

File (java.io.File)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)2 MultipartEntityBuilder (org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder)2 NameValuePair (org.apache.hc.core5.http.NameValuePair)2 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)2 U (com.weicoder.common.U)1 W (com.weicoder.common.W)1 HttpConstants (com.weicoder.common.constants.HttpConstants)1 StringConstants (com.weicoder.common.constants.StringConstants)1 IOUtil (com.weicoder.common.io.IOUtil)1 Lists (com.weicoder.common.lang.Lists)1 Log (com.weicoder.common.log.Log)1 LogFactory (com.weicoder.common.log.LogFactory)1 List (java.util.List)1 Map (java.util.Map)1 HttpMultipartMode (org.apache.hc.client5.http.entity.mime.HttpMultipartMode)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1