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


Java MultipartRequestEntity类代码示例

本文整理汇总了Java中org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity的典型用法代码示例。如果您正苦于以下问题:Java MultipartRequestEntity类的具体用法?Java MultipartRequestEntity怎么用?Java MultipartRequestEntity使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MultipartRequestEntity类属于org.apache.commons.httpclient.methods.multipart包,在下文中一共展示了MultipartRequestEntity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createMultiPartRequestContentWithDataTest

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
/**
 * Test 
 * 
 * @throws  Exception
 *          Any exception
 */
@Test
public void createMultiPartRequestContentWithDataTest() throws Exception
{
    String string="FILE_DATA";
    File file=File.createTempFile("temp_",".txt");
    file.deleteOnExit();
    byte[] input=string.getBytes(IOHelper.getDefaultEncoding());
    IOHelper.writeFile(input,file);

    HTTPRequest httpRequest=new HTTPRequest();
    ContentPart<?>[] parts=new ContentPart[5];  //make array bigger to simulate null parts
    parts[0]=new ContentPart<String>("string_part","TEST_DATA",ContentPartType.STRING);
    parts[1]=new ContentPart<String>("string_part2","TEST_DATA2",ContentPartType.STRING);
    parts[2]=new ContentPart<File>("file_part",file,ContentPartType.FILE);
    httpRequest.setContent(parts);
    HttpMethodBase method=this.client.createMethod("http://fax4j.org",HTTPMethod.POST);
    RequestEntity output=this.client.createMultiPartRequestContent(httpRequest,method);
    
    file.delete();

    Assert.assertNotNull(output);
    Assert.assertEquals(MultipartRequestEntity.class,output.getClass());
}
 
开发者ID:sagiegurari,项目名称:fax4j,代码行数:30,代码来源:ApacheHTTPClientTest.java

示例2: testPostFilePart

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的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

示例3: buildMultipartPostRequest

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
public PostRequest buildMultipartPostRequest(File file, String filename, String siteId, String containerId) throws IOException
{
    Part[] parts = 
        { 
            new FilePart("filedata", file.getName(), file, "text/plain", null), 
            new StringPart("filename", filename),
            new StringPart("description", "description"), 
            new StringPart("siteid", siteId), 
            new StringPart("containerid", containerId) 
        };

    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new HttpMethodParams());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(os);

    PostRequest postReq = new PostRequest(UPLOAD_URL, os.toByteArray(), multipartRequestEntity.getContentType());
    return postReq;
}
 
开发者ID:Alfresco,项目名称:alfresco-remote-api,代码行数:20,代码来源:UploadWebScriptTest.java

示例4: testPostStringPart

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
/**
 * Test that the body consisting of a string part can be posted.
 */
public void testPostStringPart() throws Exception {
    
    this.server.setHttpService(new EchoService());
    
    PostMethod method = new PostMethod();
    MultipartRequestEntity entity = new MultipartRequestEntity(
        new Part[] { new StringPart("param", "Hello", "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=\"param\"") >= 0);
    assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
    assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
    assertTrue(body.indexOf("Hello") >= 0);
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:22,代码来源:TestMultipartPost.java

示例5: testPostFilePartUnknownLength

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
public void testPostFilePartUnknownLength() throws Exception {
    
    this.server.setHttpService(new EchoService());

    String enc = "ISO-8859-1";
    PostMethod method = new PostMethod();
    byte[] content = "Hello".getBytes(enc);
    MultipartRequestEntity entity = new MultipartRequestEntity(
        new Part[] { 
            new FilePart(
                "param1", 
                new TestPartSource("filename.txt", content), 
                 "text/plain", 
                 enc) },
         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="+enc) >= 0);
    assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
    assertTrue(body.indexOf("Hello") >= 0);
}
 
开发者ID:jenkinsci,项目名称:lib-commons-httpclient,代码行数:27,代码来源:TestMultipartPost.java

示例6: processRequest

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
private TeleplanResponse processRequest(String url,Part[] parts){
    TeleplanResponse tr = null;
    try{ 
        PostMethod filePost = new PostMethod(url); 
        filePost.setRequestEntity( new MultipartRequestEntity(parts, filePost.getParams()) );  
        httpclient.executeMethod(filePost);
        
        InputStream in = filePost.getResponseBodyAsStream();
        tr = new TeleplanResponse();
        tr.processResponseStream(in); 
        TeleplanResponseDAO trDAO = new TeleplanResponseDAO();
        trDAO.save(tr);

        }catch(Exception e){
            MiscUtils.getLogger().error("Error", e);
        }
        return tr; 
}
 
开发者ID:williamgrosset,项目名称:OSCAR-ConCert,代码行数:19,代码来源:TeleplanAPI.java

示例7: testSendWithMultipart

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
/** Test whether the HTTP sender able to send the HTTP header with multi-part to our monitor successfully. */
public void testSendWithMultipart() throws Exception 
{
	this.target = new HttpSender(this.testClassLogger, new KVPairData(0)){

		public HttpMethod onCreateRequest() throws Exception 
		{
			PostMethod method = new PostMethod("http://localhost:1999");
			Part[] parts = {
				new StringPart("testparamName", "testparamValue")
			};
			method.setRequestEntity(
				new MultipartRequestEntity(parts, method.getParams())
			);
			return method;
		}		
	};
	this.assertSend();		
}
 
开发者ID:cecid,项目名称:hermes,代码行数:20,代码来源:HttpSenderUnitTest.java

示例8: testSendMultiPartForm

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
@Test
public void testSendMultiPartForm() throws Exception {
    HttpClient httpclient = new HttpClient();
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test");
    Part[] parts = {
        new StringPart("comment", "A binary file of some kind"),
        new FilePart(file.getName(), file)
    };

    MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
    httppost.setRequestEntity(reqEntity);

    int status = httpclient.executeMethod(httppost);

    assertEquals("Get a wrong response status", 200, status);

    String result = httppost.getResponseBodyAsString();
    assertEquals("Get a wrong result", "A binary file of some kind", result);
    assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:22,代码来源:MultiPartFormWithCustomFilterTest.java

示例9: testSendMultiPartFormOverrideEnableMultpartFilterFalse

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
@Test
public void testSendMultiPartFormOverrideEnableMultpartFilterFalse() throws Exception {
    HttpClient httpclient = new HttpClient();

    File file = new File("src/main/resources/META-INF/NOTICE.txt");

    PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test2");
    Part[] parts = {
        new StringPart("comment", "A binary file of some kind"),
        new FilePart(file.getName(), file)
    };

    MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
    httppost.setRequestEntity(reqEntity);

    int status = httpclient.executeMethod(httppost);

    assertEquals("Get a wrong response status", 200, status);
    assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:21,代码来源:MultiPartFormWithCustomFilterTest.java

示例10: testHttpClient

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
@Test
public void testHttpClient() throws Exception {
    File jpg = new File("src/test/resources/java.jpg");
    String body = "TEST";
    Part[] parts = new Part[] {new StringPart("body", body), new FilePart(jpg.getName(), jpg)};
    
    PostMethod method = new PostMethod("http://localhost:" + port2 + "/test/hello");
    MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
    method.setRequestEntity(requestEntity);
    
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    
    String responseBody = method.getResponseBodyAsString();
    assertEquals(body, responseBody);
    
    String numAttachments = method.getResponseHeader("numAttachments").getValue();
    assertEquals(numAttachments, "2");
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:20,代码来源:HttpBridgeMultipartRouteTest.java

示例11: setRequestContentMultiPartDataTest

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
/**
 * Test 
 * 
 * @throws  Exception
 *          Any exception
 */
@Test
public void setRequestContentMultiPartDataTest() throws Exception
{
    HTTPRequest httpRequest=new HTTPRequest();
    ContentPart<?>[] parts=new ContentPart[3];  //make array bigger to simulate null parts
    parts[0]=new ContentPart<String>("string_part","TEST_DATA",ContentPartType.STRING);
    httpRequest.setContent(parts);
    PostMethod method=(PostMethod)this.client.createMethod("http://fax4j.org",HTTPMethod.POST);
    this.client.setRequestContent(httpRequest,method);
    RequestEntity output=method.getRequestEntity();
    Assert.assertNotNull(output);
    Assert.assertEquals(MultipartRequestEntity.class,output.getClass());
}
 
开发者ID:sagiegurari,项目名称:fax4j,代码行数:20,代码来源:ApacheHTTPClientTest.java

示例12: sendFile

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
public static int sendFile(final String host, final String port, final String path, final String fileName,
    final InputStream inputStream, final long lengthInBytes) {
  HttpClient client = new HttpClient();
  try {

    client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    client.getParams().setSoTimeout(3600 * 1000); // One hour
    PostMethod post = new PostMethod("http://" + host + ":" + port + "/" + path);
    Part[] parts = { new FilePart(fileName, new PartSource() {
      @Override
      public long getLength() {
        return lengthInBytes;
      }

      @Override
      public String getFileName() {
        return "fileName";
      }

      @Override
      public InputStream createInputStream() throws IOException {
        return new BufferedInputStream(inputStream);
      }
    }) };
    post.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams()));
    client.executeMethod(post);
    if (post.getStatusCode() >= 400) {
      String errorString = "POST Status Code: " + post.getStatusCode() + "\n";
      if (post.getResponseHeader("Error") != null) {
        errorString += "ServletException: " + post.getResponseHeader("Error").getValue();
      }
      throw new HttpException(errorString);
    }
    return post.getStatusCode();
  } catch (Exception e) {
    LOGGER.error("Caught exception while sending file", e);
    Utils.rethrowException(e);
    throw new AssertionError("Should not reach this");
  }
}
 
开发者ID:Hanmourang,项目名称:Pinot,代码行数:41,代码来源:FileUploadUtils.java

示例13: testUploadFile

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
@Test
public void testUploadFile() throws IOException, URISyntaxException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getNodeURI()).path("files").build();

    // create single page
    final URL fileUrl = RepositoryEntriesITCase.class.getResource("singlepage.html");
    assertNotNull(fileUrl);
    final File file = new File(fileUrl.toURI());

    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", file), new StringPart("filename", file.getName()) };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
    final int code = c.executeMethod(method);
    assertEquals(code, 200);

    final OlatNamedContainerImpl folder = BCCourseNode.getNodeFolderContainer((BCCourseNode) bcNode, course1.getCourseEnvironment());
    final VFSItem item = folder.resolve(file.getName());
    assertNotNull(item);
}
 
开发者ID:huihoo,项目名称:olat,代码行数:23,代码来源:CoursesFoldersITCase.java

示例14: getMultipartRequest

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的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

示例15: getExtractivProcessString

import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //导入依赖的package包/类
private static PostMethod getExtractivProcessString(
                            final URI extractivURI, 
                            final String content,
                            final String serviceKey) throws FileNotFoundException 
{

    final PartBase filePart = new StringPart("content", content, null);

    // bytes to upload
    final ArrayList<Part> message = new ArrayList<Part>();
    message.add(filePart);
    message.add(new StringPart("formids", "content"));
    message.add(new StringPart("output_format", "JSON"));
    message.add(new StringPart("api_key", serviceKey));
   
    final Part[] messageArray = message.toArray(new Part[0]);

    // Use a Post for the file upload
    final PostMethod postMethod = new PostMethod(extractivURI.toString());
    postMethod.setRequestEntity(new MultipartRequestEntity(messageArray, postMethod.getParams()));
    postMethod.addRequestHeader("Accept-Encoding", "gzip"); // Request the response be compressed (this is highly recommended)

    return postMethod;
}
 
开发者ID:NERD-project,项目名称:nerd-api,代码行数:25,代码来源:ExtractivClient.java


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