public void writeRequest(OutputStream out) throws IOException { Part.sendParts(out, parts, getMultipartBoundary()); }
/** * Returns <code>true</code> if all parts are repeatable, <code>false</code> otherwise. * @see org.apache.commons.httpclient.methods.RequestEntity#isRepeatable() */ public boolean isRepeatable() { for (int i = 0; i < parts.length; i++) { if (!parts[i].isRepeatable()) { return false; } } return true; }
/** * Return a string representation of this object. * @return A string representation of this object. * @see java.lang.Object#toString() */ public String toString() { return this.getName(); }
public static PostMethod sendMultipartPostRequest(String url, String body) throws IOException { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); // our handlers ignore key...so we can put anything here Part[] parts = {new StringPart("body", body)}; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); httpClient.executeMethod(postMethod); return postMethod; }
/** * Adds a text field part * * @param parameterName The name of the parameter. * @param parameterValue The value of the parameter. */ public void addParameter(String parameterName, String parameterValue) { LOG.trace("enter addParameter(String parameterName, String parameterValue)"); Part param = new StringPart(parameterName, parameterValue); parameters.add(param); }
/** * FilePart Constructor. * * @param name the name of the file part * @param file the file to post * * @throws FileNotFoundException if the <i>file</i> is not a normal * file or if it is not readable. */ public FilePart(String name, File file) throws FileNotFoundException { this(name, new FilePartSource(file), null, null); }
public long getContentLength() { try { return Part.getLengthOfParts(parts, getMultipartBoundary()); } catch (Exception e) { log.error("An exception occurred while getting the length of the parts", e); return 0; } }
public void setCharSet(String charSet) { super.setCharSet(charSet); this.content = null; }
/** * Return the total sum of all parts and that of the last boundary * * @param parts The parts. * @return The total length * * @throws IOException If an I/O error occurs while writing the parts. */ public static long getLengthOfParts(Part[] parts) throws IOException { return getLengthOfParts(parts, DEFAULT_BOUNDARY_BYTES); }
/** * Return the length of the data. * @return The length of the data. * @throws IOException If an IO problem occurs * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData() */ protected long lengthOfData() throws IOException { LOG.trace("enter lengthOfData()"); return getContent().length; }
/** * Write all parts and the last boundary to the specified output stream. * * @param out The stream to write to. * @param parts The parts to write. * * @throws IOException If an I/O error occurs while writing the parts. */ public static void sendParts(OutputStream out, final Part[] parts) throws IOException { sendParts(out, parts, DEFAULT_BOUNDARY_BYTES); }
/** * Return the length of the data. * @return The length. * @throws IOException if an IO problem occurs * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData() */ protected long lengthOfData() throws IOException { LOG.trace("enter lengthOfData()"); return source.getLength(); }
/** * Write the start to the specified output stream * @param out The output stream * @throws IOException If an IO problem occurs. */ protected void sendStart(OutputStream out) throws IOException { LOG.trace("enter sendStart(OutputStream out)"); out.write(EXTRA_BYTES); out.write(getPartBoundary()); out.write(CRLF_BYTES); }
/** * Gets the content in bytes. Bytes are lazily created to allow the charset to be changed * after the part is created. * * @return the content in bytes */ private byte[] getContent() { if (content == null) { content = EncodingUtil.getBytes(value, getCharSet()); } return content; }
public static PutMethod sendMultipartPutRequest(String url, String body) throws IOException { HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(url); // our handlers ignore key...so we can put anything here Part[] parts = {new StringPart("body", body)}; putMethod.setRequestEntity(new MultipartRequestEntity(parts, putMethod.getParams())); httpClient.executeMethod(putMethod); return putMethod; } }
/** * FilePart Constructor. * * @param name the name of the file part * @param fileName the file name * @param file the file to post * * @throws FileNotFoundException if the <i>file</i> is not a normal * file or if it is not readable. */ public FilePart(String name, String fileName, File file) throws FileNotFoundException { this(name, new FilePartSource(fileName, file), null, null); }
/** * <p>Return the length of the request body.</p> * * <p>Once this method has been invoked, the request parameters cannot be * altered until the method is {@link #recycle recycled}.</p> * * @return The request content length. */ protected long getRequestContentLength() throws IOException { LOG.trace("enter MultipartPostMethod.getRequestContentLength()"); return Part.getLengthOfParts(getParts()); }
/** * Writes the data to the given OutputStream. * @param out the OutputStream to write to * @throws IOException if there is a write error */ protected void sendData(OutputStream out) throws IOException { LOG.trace("enter sendData(OutputStream)"); out.write(getContent()); }
/** * FilePart Constructor. * * @param name the name of the file part * @param file the file to post * @param contentType the content type for this part, if <code>null</code> the * {@link #DEFAULT_CONTENT_TYPE default} is used * @param charset the charset encoding for this part, if <code>null</code> the * {@link #DEFAULT_CHARSET default} is used * * @throws FileNotFoundException if the <i>file</i> is not a normal * file or if it is not readable. */ public FilePart(String name, File file, String contentType, String charset) throws FileNotFoundException { this(name, new FilePartSource(file), contentType, charset); }
/** * FilePart Constructor. * * @param name the name of the file part * @param fileName the file name * @param file the file to post * @param contentType the content type for this part, if <code>null</code> the * {@link #DEFAULT_CONTENT_TYPE default} is used * @param charset the charset encoding for this part, if <code>null</code> the * {@link #DEFAULT_CHARSET default} is used * * @throws FileNotFoundException if the <i>file</i> is not a normal * file or if it is not readable. */ public FilePart(String name, String fileName, File file, String contentType, String charset) throws FileNotFoundException { this(name, new FilePartSource(fileName, file), contentType, charset); }