Java Code Examples for org.apache.http.entity.mime.FormBodyPart#addField()

The following examples show how to use org.apache.http.entity.mime.FormBodyPart#addField() . 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: MultipartFormDataParserTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileUploadWithEagerParsingAndNonASCIIFilename() throws Exception {
    DefaultServer.setRootHandler(new EagerFormParsingHandler().setNext(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {

        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        MultipartEntity entity = new MultipartEntity();

        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));

        File uploadfile = new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile());
        FormBodyPart filePart = new FormBodyPart("file", new FileBody(uploadfile, "τεστ", "application/octet-stream", Charsets.UTF_8.toString()));
        filePart.addField("Content-Disposition", "form-data; name=\"file\"; filename*=\"utf-8''%CF%84%CE%B5%CF%83%CF%84.txt\"");
        entity.addPart(filePart);

        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);


    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example 2
Source File: MultipartEntityBuilder.java    From iaf with Apache License 2.0 5 votes vote down vote up
public MultipartEntityBuilder addPart(FormBodyPart bodyPart) {
	if (bodyPart == null) {
		return this;
	}
	if (this.bodyParts == null) {
		this.bodyParts = new ArrayList<FormBodyPart>();
	}

	if(mtom) {
		Header header = bodyPart.getHeader();
		String contentID;
		String fileName = bodyPart.getBody().getFilename();
		header.removeFields("Content-Disposition");
		if(fileName == null) {
			contentID = "<"+bodyPart.getName()+">";
		}
		else {
			bodyPart.addField("Content-Disposition", "attachment; name=\""+bodyPart.getName()+"\"; filename=\""+fileName+"\"");
			contentID = "<"+fileName+">";
		}
		bodyPart.addField("Content-ID", contentID);

		if(firstPart == null)
			firstPart = contentID;
	}

	this.bodyParts.add(bodyPart);
	return this;
}