本文整理汇总了Java中org.apache.commons.httpclient.methods.multipart.Part类的典型用法代码示例。如果您正苦于以下问题:Java Part类的具体用法?Java Part怎么用?Java Part使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Part类属于org.apache.commons.httpclient.methods.multipart包,在下文中一共展示了Part类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPostFilePart
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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.Part; //导入依赖的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: addFormDataPart
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的package包/类
private void addFormDataPart(List<Part> parts, RequestableHttpVariable httpVariable, Object httpVariableValue) throws FileNotFoundException {
String stringValue = ParameterUtils.toString(httpVariableValue);
if (httpVariable.getDoFileUploadMode() == DoFileUploadMode.multipartFormData) {
String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName());
File file = new File(filepath);
if (file.exists()) {
String charset = httpVariable.getDoFileUploadCharset();
String type = httpVariable.getDoFileUploadContentType();
if (org.apache.commons.lang3.StringUtils.isBlank(type)) {
type = Engine.getServletContext().getMimeType(file.getName());
}
parts.add(new FilePart(httpVariable.getHttpName(), file.getName(), file, type, charset));
} else {
throw new FileNotFoundException(filepath);
}
} else {
parts.add(new StringPart(httpVariable.getHttpName(), stringValue));
}
}
示例4: buildMultipartPostRequest
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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;
}
示例5: addContentTypeRequestHeader
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的package包/类
/**
* Adds a <tt>Content-Type</tt> request header.
*
* @param state current state of http requests
* @param conn the connection to use for I/O
*
* @throws IOException if an I/O (transport) error occurs. Some transport exceptions
* can be recovered from.
* @throws HttpException if a protocol exception occurs. Usually protocol exceptions
* cannot be recovered from.
*
* @since 3.0
*/
protected void addContentTypeRequestHeader(HttpState state,
HttpConnection conn)
throws IOException, HttpException {
LOG.trace("enter EntityEnclosingMethod.addContentTypeRequestHeader("
+ "HttpState, HttpConnection)");
if (!parameters.isEmpty()) {
StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
if (Part.getBoundary() != null) {
buffer.append("; boundary=");
buffer.append(Part.getBoundary());
}
setRequestHeader("Content-Type", buffer.toString());
}
}
示例6: testPostStringPart
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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);
}
示例7: testPostFilePartUnknownLength
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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);
}
示例8: processRequest
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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;
}
示例9: putAsciiFile
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的package包/类
/**
*Procedure parameters: Filename which is a string representing a file on the local system
*Parameters to TeleplanBroker are
*ExternalAction = "AputAscii"
*This is a RFC1867 Multi-part post
*Results from TeleplanBroker are:
* "SUCCESS"
* "FAILURE"
*/
public TeleplanResponse putAsciiFile(File f) throws FileNotFoundException{
Part[] parts = { new StringPart("ExternalAction", "AputAscii"), new FilePart("submitASCII", f) };
return processRequest(CONTACT_URL,parts);
// my ($filename) = @_;
// my $request = POST $WEBBASE, Content_type => 'form-data',
// Content => ['submitASCII' => [ $filename ],
// 'ExternalAction' => 'AputAscii'
// ];
// my $retVal = processRequest($request);
// if ($retVal == $SUCCESS)
// {#valid response
// if ($Result ne "SUCCESS")
// {
// $retVal = $VALID;
// }
// }
// else
// {
// $retVal = $ERROR;
// }
// return $retVal;
}
示例10: putMSPFile
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的package包/类
/**
*Procedure parameters: Filename which is a string representing a file on the local system
*Parameters to TeleplanBroker are
*ExternalAction = "AputRemit"
*This is a RFC1867 Multi-part post
*Results from TeleplanBroker are:
* "SUCCESS"
* "FAILURE"
*/
public TeleplanResponse putMSPFile(File f) throws FileNotFoundException {
Part[] parts = { new StringPart("ExternalAction", "AputRemit"), new FilePart("submitFile", f) };
return processRequest(CONTACT_URL,parts);
//
// my ($filename) = @_;
// my $request = POST $WEBBASE, Content_Type => 'form-data',
// Content => ['submitFile' => [ $filename ],
// 'ExternalAction' => 'AputRemit'
// ];
// my $retVal = processRequest($request);
// if ($retVal == $SUCCESS)
// {#valid response
// if ($Result ne "SUCCESS")
// {
// $retVal = $VALID;
// }
// }
// else
// {
// $retVal = $ERROR;
// }
// return $retVal;
// return null;
}
示例11: testSendWithMultipart
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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();
}
示例12: testSendMultiPartForm
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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"));
}
示例13: testSendMultiPartFormOverrideEnableMultpartFilterFalse
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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"));
}
示例14: testHttpClient
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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");
}
示例15: sendFile
import org.apache.commons.httpclient.methods.multipart.Part; //导入依赖的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");
}
}