Java Code Examples for org.apache.http.entity.mime.MultipartEntityBuilder#setCharset()

The following examples show how to use org.apache.http.entity.mime.MultipartEntityBuilder#setCharset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtil.java    From common-project with Apache License 2.0 6 votes vote down vote up
public static String sendPostFile(String url, File file, Map<String, String> data) {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    RequestConfig requestConfig =
        RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000).build();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setConfig(requestConfig);
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
    multipartEntityBuilder.addBinaryBody("file", file);
    if (data != null) {
        data.forEach((k, v) -> {
            multipartEntityBuilder.addTextBody(k, v);
        });
    }
    HttpEntity httpEntity = multipartEntityBuilder.build();
    httpPost.setEntity(httpEntity);
    try {
        CloseableHttpResponse response = httpClient.execute(httpPost);
        return EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 6 votes vote down vote up
@Override
public File execute(UploadStickerFile uploadStickerFile) throws TelegramApiException {
    assertParamNotNull(uploadStickerFile, "uploadStickerFile");
    uploadStickerFile.validate();
    try {
        String url = getBaseUrl() + UploadStickerFile.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(UploadStickerFile.USERID_FIELD, uploadStickerFile.getUserId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, uploadStickerFile.getPngSticker(), UploadStickerFile.PNGSTICKER_FIELD, true);
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return uploadStickerFile.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to upload new sticker file", e);
    }
}
 
Example 3
Source File: FileUploadParser.java    From clouddisk with MIT License 6 votes vote down vote up
public HttpPost initRequest(final FileUploadParameter parameter) {
	final FileUploadAddress fileUploadAddress = getDependResult(FileUploadAddress.class);
	final HttpPost request = new HttpPost("http://" + fileUploadAddress.getData().getUp() + CONST.URI_PATH);
	final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
	multipartEntity.setCharset(Consts.UTF_8);
	multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
	multipartEntity.addPart(CONST.QID_NAME, new StringBody(getLoginInfo().getQid(), ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("ofmt", new StringBody("json", ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("method", new StringBody("Upload.web", ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("token", new StringBody(readCookieStoreValue("token"), ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("v", new StringBody("1.0.1", ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("tk", new StringBody(fileUploadAddress.getData().getTk(), ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("Upload", new StringBody("Submit Query", ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("devtype", new StringBody("web", ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("pid", new StringBody("ajax", ContentType.DEFAULT_BINARY));
	multipartEntity.addPart("Filename",
			new StringBody(parameter.getUploadFile().getName(), ContentType.APPLICATION_JSON));
	multipartEntity.addPart("path", new StringBody(parameter.getPath(), ContentType.APPLICATION_JSON));// 解决中文不识别问题
	multipartEntity.addBinaryBody("file", parameter.getUploadFile());
	request.setEntity(multipartEntity.build());
	return request;
}
 
Example 4
Source File: ClassTest.java    From clouddisk with MIT License 6 votes vote down vote up
public static void main(String args[]){
    HttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://localhost:8080/sso-web/upload");
    httpPost.addHeader("Range","bytes=10000-");
    final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
    multipartEntity.setCharset(Consts.UTF_8);
    multipartEntity.setMode(HttpMultipartMode.STRICT);
    multipartEntity.addBinaryBody("file", new File("/Users/huanghuanlai/Desktop/test.java"));
    httpPost.setEntity(multipartEntity.build());
    try {
        HttpResponse response = httpClient.execute(httpPost);

    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 6 votes vote down vote up
@Override
public Boolean execute(SetStickerSetThumb setStickerSetThumb) throws TelegramApiException {
    assertParamNotNull(setStickerSetThumb, "setStickerSetThumb");
    setStickerSetThumb.validate();
    try {
        String url = getBaseUrl() + AddStickerToSet.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SetStickerSetThumb.USERID_FIELD, setStickerSetThumb.getUserId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(SetStickerSetThumb.NAME_FIELD, setStickerSetThumb.getName(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, setStickerSetThumb.getThumb(), SetStickerSetThumb.THUMB_FIELD, true);
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return setStickerSetThumb.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to add sticker to set", e);
    }
}
 
Example 6
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 6 votes vote down vote up
@Override
public Boolean execute(SetChatPhoto setChatPhoto) throws TelegramApiException {
    assertParamNotNull(setChatPhoto, "setChatPhoto");
    setChatPhoto.validate();

    try {
        String url = getBaseUrl() + SetChatPhoto.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SetChatPhoto.CHATID_FIELD, setChatPhoto.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        if (setChatPhoto.getPhoto() != null) {
            builder.addBinaryBody(SetChatPhoto.PHOTO_FIELD, setChatPhoto.getPhoto());
        } else if (setChatPhoto.getPhotoStream() != null) {
            builder.addBinaryBody(SetChatPhoto.PHOTO_FIELD, setChatPhoto.getPhotoStream(), ContentType.APPLICATION_OCTET_STREAM, setChatPhoto.getPhotoName());
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return setChatPhoto.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to set chat photo", e);
    }
}
 
Example 7
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public final Message execute(SendSticker sendSticker) throws TelegramApiException {
    assertParamNotNull(sendSticker, "sendSticker");

    sendSticker.validate();
    try {
        String url = getBaseUrl() + SendSticker.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, sendSticker.getSticker(), SendSticker.STICKER_FIELD, true);

        if (sendSticker.getReplyMarkup() != null) {
            builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendSticker.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendSticker.getReplyToMessageId() != null) {
            builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendSticker.getDisableNotification() != null) {
            builder.addTextBody(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return sendSticker.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to send sticker", e);
    }
}
 
Example 8
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Serializable execute(EditMessageMedia editMessageMedia) throws TelegramApiException {
    assertParamNotNull(editMessageMedia, "editMessageMedia");
    editMessageMedia.validate();
    try {
        String url = getBaseUrl() + EditMessageMedia.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        if (editMessageMedia.getInlineMessageId() == null) {
            builder.addTextBody(EditMessageMedia.CHATID_FIELD, editMessageMedia.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
            builder.addTextBody(EditMessageMedia.MESSAGEID_FIELD, editMessageMedia.getMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);

        } else {
            builder.addTextBody(EditMessageMedia.INLINE_MESSAGE_ID_FIELD, editMessageMedia.getInlineMessageId(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (editMessageMedia.getReplyMarkup() != null) {
            builder.addTextBody(EditMessageMedia.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(editMessageMedia.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }

        addInputData(builder, editMessageMedia.getMedia(), EditMessageMedia.MEDIA_FIELD, true);

        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return editMessageMedia.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to edit message media", e);
    }
}
 
Example 9
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean execute(CreateNewStickerSet createNewStickerSet) throws TelegramApiException {
    assertParamNotNull(createNewStickerSet, "createNewStickerSet");
    createNewStickerSet.validate();
    try {
        String url = getBaseUrl() + CreateNewStickerSet.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(CreateNewStickerSet.USERID_FIELD, createNewStickerSet.getUserId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(CreateNewStickerSet.NAME_FIELD, createNewStickerSet.getName(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(CreateNewStickerSet.TITLE_FIELD, createNewStickerSet.getTitle(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(CreateNewStickerSet.EMOJIS_FIELD, createNewStickerSet.getEmojis(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(CreateNewStickerSet.CONTAINSMASKS_FIELD, createNewStickerSet.getContainsMasks().toString(), TEXT_PLAIN_CONTENT_TYPE);
        if (createNewStickerSet.getPngSticker() != null) {
            addInputFile(builder, createNewStickerSet.getPngSticker(), CreateNewStickerSet.PNGSTICKER_FIELD, true);
        } else {
            addInputFile(builder, createNewStickerSet.getTgsSticker(), CreateNewStickerSet.TGSSTICKER_FIELD, true);
        }

        if (createNewStickerSet.getMaskPosition() != null) {
            builder.addTextBody(CreateNewStickerSet.MASKPOSITION_FIELD, objectMapper.writeValueAsString(createNewStickerSet.getMaskPosition()), TEXT_PLAIN_CONTENT_TYPE);
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return createNewStickerSet.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to create new sticker set", e);
    }
}
 
Example 10
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Boolean execute(AddStickerToSet addStickerToSet) throws TelegramApiException {
    assertParamNotNull(addStickerToSet, "addStickerToSet");
    addStickerToSet.validate();
    try {
        String url = getBaseUrl() + AddStickerToSet.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(AddStickerToSet.USERID_FIELD, addStickerToSet.getUserId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(AddStickerToSet.NAME_FIELD, addStickerToSet.getName(), TEXT_PLAIN_CONTENT_TYPE);
        builder.addTextBody(AddStickerToSet.EMOJIS_FIELD, addStickerToSet.getEmojis(), TEXT_PLAIN_CONTENT_TYPE);
        if (addStickerToSet.getPngSticker() != null) {
            addInputFile(builder, addStickerToSet.getPngSticker(), AddStickerToSet.PNGSTICKER_FIELD, true);
        } else {
            addInputFile(builder, addStickerToSet.getTgsSticker(), AddStickerToSet.TGSSTICKER_FIELD, true);
        }

        if (addStickerToSet.getMaskPosition() != null) {
            builder.addTextBody(AddStickerToSet.MASKPOSITION_FIELD, objectMapper.writeValueAsString(addStickerToSet.getMaskPosition()), TEXT_PLAIN_CONTENT_TYPE);
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return addStickerToSet.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to add sticker to set", e);
    }
}
 
Example 11
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public List<Message> execute(SendMediaGroup sendMediaGroup) throws TelegramApiException {
    assertParamNotNull(sendMediaGroup, "sendMediaGroup");
    sendMediaGroup.validate();

    try {
        String url = getBaseUrl() + SendMediaGroup.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendMediaGroup.CHATID_FIELD, sendMediaGroup.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputData(builder, sendMediaGroup.getMedia(), SendMediaGroup.MEDIA_FIELD);

        if (sendMediaGroup.getDisableNotification() != null) {
            builder.addTextBody(SendMediaGroup.DISABLENOTIFICATION_FIELD, sendMediaGroup.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }

        if (sendMediaGroup.getReplyToMessageId() != null) {
            builder.addTextBody(SendMediaGroup.REPLYTOMESSAGEID_FIELD, sendMediaGroup.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }


        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return sendMediaGroup.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to set chat photo", e);
    }
}
 
Example 12
Source File: Postpone.java    From FreeServer with Apache License 2.0 5 votes vote down vote up
/**
     * 提交延期记录
     * @param yunClient
     * @param url
     * @param file
     */
    public void sendAbei(UserInfo info, HttpClient yunClient, String url, File file, Map<String,String> cookieMap, int bz) throws Exception {
        log.info("开始提交延期记录!!!");
//        HttpPost httpPost = new HttpPost(CommCode.getYunUrl(bz,1));
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

        //三丰云和阿贝云文件提交方式不一样
        if(bz==0) {
            multipartEntityBuilder.addBinaryBody("yanqi_img",file);
        }else{
            multipartEntityBuilder.addBinaryBody("yanqi_img",file, ContentType.IMAGE_PNG,"postpone.png");
        }
        multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
        ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8"));
        multipartEntityBuilder.addTextBody("cmd","free_delay_add" ,contentType);
        multipartEntityBuilder.addTextBody("ptype","vps" ,contentType);
        multipartEntityBuilder.addTextBody("url",url ,contentType);
        String postRes = HttpUtil.getPostRes(yunClient, ParamUtil.getYunUrl(bz,1), multipartEntityBuilder.build());

        JSONObject json = JSONObject.fromObject(postRes);
        log.info("提交延期记录返回结果:"+json.toString());
        try{
            if("提交成功".equals(json.getString("msg"))){
                log.info("提交延期记录成功!!!");
            }else{
                log.info("提交延期记录失败,删除发布博客!!!");
                Sina.deleteBlog(info,url,cookieMap);
            }
        }catch (Exception e){
            log.info("提交延期记录失败,删除发布博客!!!");
            Sina.deleteBlog(info,url,cookieMap);
        }
        file.delete();

    }
 
Example 13
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public final Message execute(SendPhoto sendPhoto) throws TelegramApiException {
    assertParamNotNull(sendPhoto, "sendPhoto");

    sendPhoto.validate();
    try {
        String url = getBaseUrl() + SendPhoto.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, sendPhoto.getPhoto(), SendPhoto.PHOTO_FIELD, true);

        if (sendPhoto.getReplyMarkup() != null) {
            builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendPhoto.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendPhoto.getReplyToMessageId() != null) {
            builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendPhoto.getCaption() != null) {
            builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
            if (sendPhoto.getParseMode() != null) {
                builder.addTextBody(SendPhoto.PARSEMODE_FIELD, sendPhoto.getParseMode(), TEXT_PLAIN_CONTENT_TYPE);
            }
        }
        if (sendPhoto.getDisableNotification() != null) {
            builder.addTextBody(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return sendPhoto.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to send photo", e);
    }
}
 
Example 14
Source File: JapiClientTransfer.java    From japi with MIT License 5 votes vote down vote up
private Result postFile(String url, List<String[]> datas, File file) {
    if (!file.exists()) {
        throw new JapiException(file.getAbsolutePath() + " file not exist.");
    }
    HttpPost httpPost = new HttpPost(url);
    Integer tryCount = 0;
    while (reties > tryCount) {
        try {
            final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            multipartEntity.setCharset(Charset.forName("utf-8"));
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (String[] nameValue : datas) {
                multipartEntity.addPart(nameValue[0], new StringBody(nameValue[1], ContentType.APPLICATION_JSON));
            }
            multipartEntity.addBinaryBody("file", file);
            httpPost.setEntity(multipartEntity.build());
            httpPost.setHeader("token", token);
            String result = EntityUtils.toString(HTTP_CLIENT.execute(httpPost).getEntity());
            Result result1 = JSON.parseObject(result, ResultImpl.class);
            if (result1.getCode() != 0) {
                throw new JapiException(result1.getMsg());
            }
            return result1;
        } catch (IOException e) {
            if (e instanceof HttpHostConnectException) {
                tryCount++;
                LOGGER.warn("try connect server " + tryCount + " count.");
                try {
                    TimeUnit.SECONDS.sleep(tryTime);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    if (tryCount <= reties) {
        LOGGER.error("server connect failed.");
    }
    return null;
}
 
Example 15
Source File: HttpClientWrapper.java    From TrackRay with GNU General Public License v3.0 5 votes vote down vote up
/**
 * POST方式发送名值对请求URL,上传文件(包括图片)
 *
 * @param url
 * @return
 * @throws HttpException
 * @throws IOException
 */
public ResponseStatus postEntity(String url, String urlEncoding) throws HttpException, IOException {
    if (url == null)
        return null;

    HttpEntity entity = null;
    HttpRequestBase request = null;
    CloseableHttpResponse response = null;
    try {
        this.parseUrl(url);
        HttpPost httpPost = new HttpPost(toUrl());

        //对请求的表单域进行填充
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        for (NameValuePair nameValuePair : this.getNVBodies()) {
            entityBuilder.addPart(nameValuePair.getName(),
                    new StringBody(nameValuePair.getValue(), ContentType.create("text/plain", urlEncoding)));
        }
        for (ContentBody contentBody : getContentBodies()) {
            entityBuilder.addPart("file", contentBody);
        }
        entityBuilder.setCharset(CharsetUtils.get(urlEncoding));
        httpPost.setEntity(entityBuilder.build());
        request = httpPost;
        response = client.execute(request);

        //响应状态
        StatusLine statusLine = response.getStatusLine();
        // 获取响应对象
        entity = response.getEntity();
        ResponseStatus ret = new ResponseStatus();
        ret.setStatusCode(statusLine.getStatusCode());
        getResponseStatus(entity, ret);
        return ret;
    } finally {
        close(entity, request, response);
    }
}
 
Example 16
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 4 votes vote down vote up
/**
 * Sends a file using Send Audio method (https://core.telegram.org/bots/api#sendaudio)
 * @param sendAudio Information to send
 * @return If success, the sent Message is returned
 * @throws TelegramApiException If there is any error sending the audio
 */
@Override
public final Message execute(SendAudio sendAudio) throws TelegramApiException {
    assertParamNotNull(sendAudio, "sendAudio");
    sendAudio.validate();
    try {
        String url = getBaseUrl() + SendAudio.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, sendAudio.getAudio(), SendAudio.AUDIO_FIELD, true);

        if (sendAudio.getReplyMarkup() != null) {
            builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendAudio.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAudio.getReplyToMessageId() != null) {
            builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAudio.getPerformer() != null) {
            builder.addTextBody(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAudio.getTitle() != null) {
            builder.addTextBody(SendAudio.TITLE_FIELD, sendAudio.getTitle(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if(sendAudio.getDuration() != null){
            builder.addTextBody(SendAudio.DURATION_FIELD, sendAudio.getDuration().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAudio.getDisableNotification() != null) {
            builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAudio.getCaption() != null) {
            builder.addTextBody(SendAudio.CAPTION_FIELD, sendAudio.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
            if (sendAudio.getParseMode() != null) {
                builder.addTextBody(SendAudio.PARSEMODE_FIELD, sendAudio.getParseMode(), TEXT_PLAIN_CONTENT_TYPE);
            }
        }
        if (sendAudio.getThumb() != null) {
            addInputFile(builder, sendAudio.getThumb(), SendAudio.THUMB_FIELD, false);
            builder.addTextBody(SendAudio.THUMB_FIELD, sendAudio.getThumb().getAttachName(), TEXT_PLAIN_CONTENT_TYPE);
        }

        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);


        return sendAudio.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to send audio", e);
    }
}
 
Example 17
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public Message execute(SendAnimation sendAnimation) throws TelegramApiException {
    assertParamNotNull(sendAnimation, "sendAnimation");
    sendAnimation.validate();
    try {
        String url = getBaseUrl() + SendAnimation.PATH;
        HttpPost httppost = configuredHttpPost(url);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendAnimation.CHATID_FIELD, sendAnimation.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, sendAnimation.getAnimation(), SendAnimation.ANIMATION_FIELD, true);

        if (sendAnimation.getReplyMarkup() != null) {
            builder.addTextBody(SendAnimation.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendAnimation.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAnimation.getReplyToMessageId() != null) {
            builder.addTextBody(SendAnimation.REPLYTOMESSAGEID_FIELD, sendAnimation.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAnimation.getDisableNotification() != null) {
            builder.addTextBody(SendAnimation.DISABLENOTIFICATION_FIELD, sendAnimation.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAnimation.getDuration() != null) {
            builder.addTextBody(SendAnimation.DURATION_FIELD, sendAnimation.getDuration().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAnimation.getWidth() != null) {
            builder.addTextBody(SendAnimation.WIDTH_FIELD, sendAnimation.getWidth().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAnimation.getHeight() != null) {
            builder.addTextBody(SendAnimation.HEIGHT_FIELD, sendAnimation.getHeight().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendAnimation.getThumb() != null) {
            addInputFile(builder, sendAnimation.getThumb(), SendAnimation.THUMB_FIELD, false);
            builder.addTextBody(SendAnimation.THUMB_FIELD, sendAnimation.getThumb().getAttachName(), TEXT_PLAIN_CONTENT_TYPE);
        }

        if (sendAnimation.getCaption() != null) {
            builder.addTextBody(SendAnimation.CAPTION_FIELD, sendAnimation.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
            if (sendAnimation.getParseMode() != null) {
                builder.addTextBody(SendAnimation.PARSEMODE_FIELD, sendAnimation.getParseMode(), TEXT_PLAIN_CONTENT_TYPE);
            }
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return sendAnimation.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to edit message media", e);
    }
}
 
Example 18
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public final Message execute(SendVideoNote sendVideoNote) throws TelegramApiException {
    assertParamNotNull(sendVideoNote, "sendVideoNote");

    sendVideoNote.validate();
    try {
        String url = getBaseUrl() + SendVideoNote.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, sendVideoNote.getVideoNote(), SendVideoNote.VIDEONOTE_FIELD, true);

        if (sendVideoNote.getReplyMarkup() != null) {
            builder.addTextBody(SendVideoNote.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideoNote.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideoNote.getReplyToMessageId() != null) {
            builder.addTextBody(SendVideoNote.REPLYTOMESSAGEID_FIELD, sendVideoNote.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideoNote.getDuration() != null) {
            builder.addTextBody(SendVideoNote.DURATION_FIELD, sendVideoNote.getDuration().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideoNote.getLength() != null) {
            builder.addTextBody(SendVideoNote.LENGTH_FIELD, sendVideoNote.getLength().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideoNote.getDisableNotification() != null) {
            builder.addTextBody(SendVideoNote.DISABLENOTIFICATION_FIELD, sendVideoNote.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideoNote.getThumb() != null) {
            addInputFile(builder, sendVideoNote.getThumb(), SendVideoNote.THUMB_FIELD, false);
            builder.addTextBody(SendVideoNote.THUMB_FIELD, sendVideoNote.getThumb().getAttachName(), TEXT_PLAIN_CONTENT_TYPE);
        }
        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);


        return sendVideoNote.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to send video note", e);
    }
}
 
Example 19
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public final Message execute(SendVideo sendVideo) throws TelegramApiException {
    assertParamNotNull(sendVideo, "sendVideo");

    sendVideo.validate();
    try {
        String url = getBaseUrl() + SendVideo.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId(), TEXT_PLAIN_CONTENT_TYPE);
        addInputFile(builder, sendVideo.getVideo(), SendVideo.VIDEO_FIELD, true);

        if (sendVideo.getReplyMarkup() != null) {
            builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideo.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getReplyToMessageId() != null) {
            builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getCaption() != null) {
            builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
            if (sendVideo.getParseMode() != null) {
                builder.addTextBody(SendVideo.PARSEMODE_FIELD, sendVideo.getParseMode(), TEXT_PLAIN_CONTENT_TYPE);
            }
        }
        if (sendVideo.getSupportsStreaming() != null) {
            builder.addTextBody(SendVideo.SUPPORTSSTREAMING_FIELD, sendVideo.getSupportsStreaming().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getDuration() != null) {
            builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getWidth() != null) {
            builder.addTextBody(SendVideo.WIDTH_FIELD, sendVideo.getWidth().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getHeight() != null) {
            builder.addTextBody(SendVideo.HEIGHT_FIELD, sendVideo.getHeight().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getDisableNotification() != null) {
            builder.addTextBody(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendVideo.getThumb() != null) {
            addInputFile(builder, sendVideo.getThumb(), SendVideo.THUMB_FIELD, false);
            builder.addTextBody(SendVideo.THUMB_FIELD, sendVideo.getThumb().getAttachName(), TEXT_PLAIN_CONTENT_TYPE);
        }

        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return sendVideo.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to send video", e);
    }
}
 
Example 20
Source File: DefaultAbsSender.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public final Message execute(SendDocument sendDocument) throws TelegramApiException {
    assertParamNotNull(sendDocument, "sendDocument");

    sendDocument.validate();
    try {
        String url = getBaseUrl() + SendDocument.PATH;
        HttpPost httppost = configuredHttpPost(url);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setLaxMode();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId(), TEXT_PLAIN_CONTENT_TYPE);

        addInputFile(builder, sendDocument.getDocument(), SendDocument.DOCUMENT_FIELD, true);

        if (sendDocument.getReplyMarkup() != null) {
            builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendDocument.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendDocument.getReplyToMessageId() != null) {
            builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }
        if (sendDocument.getCaption() != null) {
            builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
            if (sendDocument.getParseMode() != null) {
                builder.addTextBody(SendDocument.PARSEMODE_FIELD, sendDocument.getParseMode(), TEXT_PLAIN_CONTENT_TYPE);
            }
        }
        if (sendDocument.getDisableNotification() != null) {
            builder.addTextBody(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString(), TEXT_PLAIN_CONTENT_TYPE);
        }

        if (sendDocument.getThumb() != null) {
            addInputFile(builder, sendDocument.getThumb(), SendDocument.THUMB_FIELD, false);
            builder.addTextBody(SendDocument.THUMB_FIELD, sendDocument.getThumb().getAttachName(), TEXT_PLAIN_CONTENT_TYPE);
        }

        HttpEntity multipart = builder.build();
        httppost.setEntity(multipart);

        return sendDocument.deserializeResponse(sendHttpPostRequest(httppost));
    } catch (IOException e) {
        throw new TelegramApiException("Unable to send document", e);
    }
}