本文整理汇总了Java中org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource类的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayPartSource类的具体用法?Java ByteArrayPartSource怎么用?Java ByteArrayPartSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ByteArrayPartSource类属于org.apache.commons.httpclient.methods.multipart包,在下文中一共展示了ByteArrayPartSource类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPostFilePart
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
/**
* Test that the body consisting of a file part can be posted.
*/
public void testPostFilePart() throws Exception {
this.server.setHttpService(new EchoService());
PostMethod method = new PostMethod();
byte[] content = "Hello".getBytes();
MultipartRequestEntity entity = new MultipartRequestEntity(
new Part[] {
new FilePart(
"param1",
new ByteArrayPartSource("filename.txt", content),
"text/plain",
"ISO-8859-1") },
method.getParams());
method.setRequestEntity(entity);
client.executeMethod(method);
assertEquals(200,method.getStatusCode());
String body = method.getResponseBodyAsString();
assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
assertTrue(body.indexOf("Hello") >= 0);
}
示例2: buildParts
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
private List<Part> buildParts(BaseRequest request) {
List<Part> parts = new ArrayList<Part>();
for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) {
if (h.getFileName() == null) {
StringPart stringPart = new StringPart(h.getPartName(), new String(h.getBodyAsString()));
if (h.isContentTypeSet()) {
stringPart.setContentType(h.getContentType());
}
parts.add(stringPart);
} else {
PartSource partSource = new ByteArrayPartSource(h.getFileName(), h.getBody());
FilePart p = new FilePart(h.getPartName(), partSource);
if (h.isContentTypeSet()) {
p.setContentType(h.getContentType());
}
parts.add(p);
}
}
return parts;
}
示例3: getMultipartRequest
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
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;
}
示例4: buildMultipartRequest
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
public HttpMethod buildMultipartRequest(URI location, Map<Path, byte[]> toUpload) throws HttpException, IOException {
PostMethod filePost = new PostMethod(location.toASCIIString());
List<Part> parts = new ArrayList<Part>(toUpload.size());
for (Entry<Path, byte[]> entry : toUpload.entrySet())
parts.add(new FilePart(entry.getKey().toString(), new ByteArrayPartSource(entry.getKey().toString(), entry.getValue())));
filePost.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), filePost.getParams()));
return filePost;
}
示例5: addFilePart
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
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));
}
示例6: testMultipart
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
@Test
public void testMultipart() throws IOException, ServletException {
String partName = "file";
String resourceName = "/testFile.txt";
byte[] fileContent = FileCopyUtils.copyToByteArray(
getClass().getResourceAsStream(resourceName));
// Create part & entity from resource
Part[] parts = new Part[] {
new FilePart(partName, new ByteArrayPartSource(resourceName, 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.setPathInfo("/multipart-test");
request.setMethod("POST");
servlet.service(request, response);
assertEquals(resourceName, response.getHeader("file-name"));
assertEquals("This is a file for multipart test.\n", response.getHeader("file-content"));
}
示例7: write
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
@Override
public void write(String name, byte[] bytes, String fileName, String contentType, String charset) throws IOException {
parts.add(new FilePart(name, new ByteArrayPartSource(fileName, bytes), contentType, charset));
}
示例8: getSimilarImagesAndIndex
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
/**
* Get similar images by vector
*
* @param vector
* @param threshold
* @return
*/
public JsonResultSet getSimilarImagesAndIndex(String id, double[] vector, double threshold) {
JsonResultSet similar = new JsonResultSet();
byte[] vectorInBytes = new byte[8 * vector.length];
ByteBuffer bbuf = ByteBuffer.wrap(vectorInBytes);
for (double value : vector) {
bbuf.putDouble(value);
}
PostMethod queryMethod = null;
String response = null;
try {
ByteArrayPartSource source = new ByteArrayPartSource("bytes", vectorInBytes);
Part[] parts = {
new StringPart("id", id),
new FilePart("vector", source),
new StringPart("threshold", String.valueOf(threshold))
};
queryMethod = new PostMethod(webServiceHost + "/rest/visual/qindex/" + collectionName);
queryMethod.setRequestEntity(new MultipartRequestEntity(parts, queryMethod.getParams()));
int code = httpClient.executeMethod(queryMethod);
if (code == 200) {
InputStream inputStream = queryMethod.getResponseBodyAsStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
response = writer.toString();
queryMethod.releaseConnection();
similar = parseResponse(response);
}
} catch (Exception e) {
_logger.error("Exception for vector of length " + vector.length, e);
response = null;
} finally {
if (queryMethod != null) {
queryMethod.releaseConnection();
}
}
return similar;
}
示例9: getSimilarImages
import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; //导入依赖的package包/类
/**
* Get similar images by vector
*
* @param vector
* @param threshold
* @return
*/
public JsonResultSet getSimilarImages(double[] vector, double threshold) {
JsonResultSet similar = new JsonResultSet();
byte[] vectorInBytes = new byte[8 * vector.length];
ByteBuffer bbuf = ByteBuffer.wrap(vectorInBytes);
for (double value : vector) {
bbuf.putDouble(value);
}
PostMethod queryMethod = null;
String response = null;
try {
ByteArrayPartSource source = new ByteArrayPartSource("bytes", vectorInBytes);
Part[] parts = {
new FilePart("vector", source),
new StringPart("threshold", String.valueOf(threshold))
};
queryMethod = new PostMethod(webServiceHost + "/rest/visual/query_vector/" + collectionName);
queryMethod.setRequestEntity(new MultipartRequestEntity(parts, queryMethod.getParams()));
int code = httpClient.executeMethod(queryMethod);
if (code == 200) {
InputStream inputStream = queryMethod.getResponseBodyAsStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer);
response = writer.toString();
queryMethod.releaseConnection();
similar = parseResponse(response);
}
} catch (Exception e) {
_logger.error("Exception for vector of length " + vector.length, e);
response = null;
} finally {
if (queryMethod != null) {
queryMethod.releaseConnection();
}
}
return similar;
}