当前位置: 首页>>代码示例>>Java>>正文


Java ByteArrayPartSource类代码示例

本文整理汇总了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);
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:29,代码来源:TestMultipartPost.java

示例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;
}
 
开发者ID:intuit,项目名称:Tank,代码行数:21,代码来源:TankHttpClient3.java

示例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;
}
 
开发者ID:lutece-platform,项目名称:lutece-core,代码行数:21,代码来源:UploadServletTest.java

示例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;
}
 
开发者ID:abstratt,项目名称:cloudfier-maven-plugin,代码行数:9,代码来源:PublisherMojo.java

示例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));
}
 
开发者ID:mendix,项目名称:RestServices,代码行数:8,代码来源:RestConsumer.java

示例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"));
}
 
开发者ID:wb14123,项目名称:bard,代码行数:28,代码来源:MultipartParamInjectorTest.java

示例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));
}
 
开发者ID:GeoinformationSystems,项目名称:GeoprocessingAppstore,代码行数:5,代码来源:HttpClientRequest.java

示例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;
}
 
开发者ID:socialsensor,项目名称:socialsensor-framework-client,代码行数:50,代码来源:VisualIndexHandler.java

示例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;
}
 
开发者ID:socialsensor,项目名称:socialsensor-framework-client,代码行数:49,代码来源:VisualIndexHandler.java


注:本文中的org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。