下面列出了怎么用org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource的API类实例代码及写法,或者点击链接到github查看源代码。
private MockHttpServletRequest getMultipartRequest( ) throws Exception
{
MockHttpServletRequest request = new MockHttpServletRequest( );
byte [ ] fileContent = new byte [ ] {
1, 2, 3
};
Part [ ] parts = new Part [ ] {
new FilePart( "file1", new ByteArrayPartSource( "file1", fileContent ) )
};
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity( parts, new PostMethod( ).getParams( ) );
// Serialize request body
ByteArrayOutputStream requestContent = new ByteArrayOutputStream( );
multipartRequestEntity.writeRequest( requestContent );
// Set request body to HTTP servlet request
request.setContent( requestContent.toByteArray( ) );
// Set content type to HTTP servlet request (important, includes Mime boundary string)
request.setContentType( multipartRequestEntity.getContentType( ) );
request.setMethod( "POST" );
return request;
}
private static void addFilePart(final IContext context, String partname,
final IMendixObject source, List<Part> parts) throws IOException {
ByteArrayPartSource p = new ByteArrayPartSource(
getFileDocumentFileName(context, source),
IOUtils.toByteArray(Core.getFileDocumentContent(context, source)));
parts.add(new FilePart(partname, p));
}
private static Part[] createSendPhotoRequestParts(TelegramBot bot, byte[] image, String imageType, String caption) {
List<Part> partList = new ArrayList<>();
partList.add(new StringPart("chat_id", bot.getChatId()));
partList.add(new FilePart("photo", new ByteArrayPartSource(String.format("image.%s", imageType), image)));
if (StringUtils.isNotBlank(caption)) {
partList.add(new StringPart("caption", caption, "UTF-8"));
}
if (StringUtils.isNotBlank(bot.getParseMode())) {
partList.add(new StringPart("parse_mode", bot.getParseMode()));
}
return partList.toArray(new Part[0]);
}
public void addFile(final int executionId, final String uid, final byte[] bytes, String fileName) throws Exception {
Part[] parts = new Part[] { new FilePart("file", new ByteArrayPartSource(fileName, bytes))};
addFile(executionId,uid,parts);
}