If you think the Android project volley listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.android.volley.toolbox.multipart;
//fromwww.java2s.comimport java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.protocol.HTTP;
/**
* @author <a href="mailto:vit at cleverua.com">Vitaliy Khudenko</a>
*/publicfinalclass FilePart extends BasePart {
privatefinalFile file;
/**
* @param name String - name of parameter (may not be <code>null</code>).
* @param file File (may not be <code>null</code>).
* @param filename String. If <code>null</code> is passed,
* then <code>file.getName()</code> is used.
* @param contentType String. If <code>null</code> is passed,
* then default "application/octet-stream" is used.
*
* @throws IllegalArgumentException if either <code>file</code>
* or <code>name</code> is <code>null</code>.
*/public FilePart(String name, File file, String filename, String contentType) {
if (file == null) {
thrownew IllegalArgumentException("File may not be null"); //$NON-NLS-1$
}
if (name == null) {
thrownew IllegalArgumentException("Name may not be null"); //$NON-NLS-1$
}
this.file = file;
final String partName = UrlEncodingHelper.encode(name, HTTP.DEFAULT_PROTOCOL_CHARSET);
final String partFilename = UrlEncodingHelper.encode(
(filename == null) ? file.getName() : filename,
HTTP.DEFAULT_PROTOCOL_CHARSET
);
final String partContentType = (contentType == null) ? HTTP.DEFAULT_CONTENT_TYPE : contentType;
headersProvider = new IHeadersProvider() {
public String getContentDisposition() {
return"Content-Disposition: form-data; name=\"" + partName //$NON-NLS-1$
+ "\"; filename=\"" + partFilename + '"'; //$NON-NLS-1$
}
public String getContentType() {
return"Content-Type: " + partContentType; //$NON-NLS-1$
}
public String getContentTransferEncoding() {
return"Content-Transfer-Encoding: binary"; //$NON-NLS-1$
}
};
}
publiclong getContentLength(Boundary boundary) {
return getHeader(boundary).length + file.length() + CRLF.length;
}
publicvoid writeTo(OutputStream out, Boundary boundary) throws IOException {
out.write(getHeader(boundary));
InputStream in = new FileInputStream(file);
try {
byte[] tmp = newbyte[4096];
int l;
while ((l = in.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
} finally {
in.close();
}
out.write(CRLF);
}
}