本文整理匯總了Java中org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity.writeRequest方法的典型用法代碼示例。如果您正苦於以下問題:Java MultipartRequestEntity.writeRequest方法的具體用法?Java MultipartRequestEntity.writeRequest怎麽用?Java MultipartRequestEntity.writeRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity
的用法示例。
在下文中一共展示了MultipartRequestEntity.writeRequest方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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;
}
示例2: 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;
}
示例3: buildMultipartPostRequest
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //導入方法依賴的package包/類
public PostRequest buildMultipartPostRequest(File file) throws IOException
{
Part[] parts = { new FilePart("filedata", file.getName(), file, "application/zip", null) };
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;
}
示例4: build
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //導入方法依賴的package包/類
public MultiPartRequest build() throws IOException
{
List<Part> parts = new ArrayList<>();
if (fileData != null)
{
FilePart fp = new FilePart("filedata", fileData.getFileName(), fileData.getFile(), fileData.getMimetype(), null);
// Get rid of the default values added upon FilePart instantiation
fp.setCharSet(fileData.getEncoding());
fp.setContentType(fileData.getMimetype());
parts.add(fp);
addPartIfNotNull(parts, "name", fileData.getFileName());
}
addPartIfNotNull(parts, "relativepath", relativePath);
addPartIfNotNull(parts, "updatenoderef", updateNodeRef);
addPartIfNotNull(parts, "description", description);
addPartIfNotNull(parts, "contenttype", contentTypeQNameStr);
addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects));
addPartIfNotNull(parts, "majorversion", majorVersion);
addPartIfNotNull(parts, "overwrite", overwrite);
addPartIfNotNull(parts, "autorename", autoRename);
addPartIfNotNull(parts, "nodetype", nodeType);
addPartIfNotNull(parts, "renditions", getCommaSeparated(renditionIds));
if (!properties.isEmpty())
{
for (Entry<String, String> prop : properties.entrySet())
{
parts.add(new StringPart(prop.getKey(), prop.getValue()));
}
}
MultipartRequestEntity req = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), new HttpMethodParams());
ByteArrayOutputStream os = new ByteArrayOutputStream();
req.writeRequest(os);
return new MultiPartRequest(os.toByteArray(), req.getContentType(), req.getContentLength());
}
示例5: testMultipart
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //導入方法依賴的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"));
}
示例6: createMultipartHttpRequest
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; //導入方法依賴的package包/類
private MockMultipartHttpServletRequest createMultipartHttpRequest(
StreamPipe streamPipe, boolean addAntiVirusParts,
boolean antiVirusLastPartFailed) throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setContentType(
"multipart/mixed;boundary=gc0p4Jq0M2Yt08jU534c0p");
List<Part> parts = new ArrayList<Part>();
String string = "<hello>test</hello>";
StringPart stringPart = new StringPart("string1", string);
parts.add(stringPart);
URL url = ClassUtils.getResourceURL(this, "/Documents/doc001.pdf");
File file = new File(url.toURI());
FilePart filePart = new FilePart("file1", file.getName(), file);
parts.add(filePart);
if (addAntiVirusParts) {
StringPart antiVirusPassedPart = new StringPart(
streamPipe.getAntiVirusPartName(),
streamPipe.getAntiVirusPassedMessage());
parts.add(antiVirusPassedPart);
}
URL url2 = ClassUtils.getResourceURL(this, "/Documents/doc002.pdf");
File file2 = new File(url2.toURI());
FilePart filePart2 = new FilePart("file2", file2.getName(), file2);
parts.add(filePart2);
if (addAntiVirusParts) {
String antiVirusLastPartMessage;
if (antiVirusLastPartFailed) {
antiVirusLastPartMessage = "Fail";
if (antiVirusLastPartMessage.equalsIgnoreCase(
streamPipe.getAntiVirusPassedMessage())) {
throw new Exception("fail message ["
+ antiVirusLastPartMessage
+ "] must differ from pass message ["
+ streamPipe.getAntiVirusPassedMessage() + "]");
}
} else {
antiVirusLastPartMessage = streamPipe
.getAntiVirusPassedMessage();
}
StringPart antiVirusPassedPart2 = new StringPart(
streamPipe.getAntiVirusPartName(),
antiVirusLastPartMessage);
parts.add(antiVirusPassedPart2);
}
Part allParts[] = new Part[parts.size()];
allParts = parts.toArray(allParts);
MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(
allParts, new PostMethod().getParams());
ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
multipartRequestEntity.writeRequest(requestContent);
request.setContent(requestContent.toByteArray());
request.setContentType(multipartRequestEntity.getContentType());
return request;
}