public class HttpsFileUploader extends Object
multipart/form-data encoding (RFC2388).
.
The class has the following features:
HttpURLConnection/HttpsURLConnection
will normally do internal buffering of the request being sent. If the file
being uploaded is large this means that Java will try to buffer all of the
file in memory. This class avoids this by streaming the file contents to the
server and therefore allows very large files to be uploaded without causing an
out-of-memory error in the JVM. If you are uploading from a stream rather than
a file then this feature does not apply.
// Configure the connection
HttpsFileUploaderConfig uploaderConfig = new HttpsFileUploaderConfig(endpointURL);
uploaderConfig.setEndpointUsername(endpointUsername);
uploaderConfig.setEndpointPassword(endpointPassword);
uploaderConfig.setValidateCertificates(false);
uploaderConfig.setProxyAddress(proxyAddress);
uploaderConfig.setProxyPort(proxyPort);
// Do the upload.
// A single file is uploaded along with a single text field.
result = HttpsFileUploader.upload(
uploaderConfig,
Collections.singletonList(UploadItemFile(new File("hugefile.zip"))),
Collections.singletonMap("email", "johnny@company.com"),
this);
// Evaluate the result.
if (!result.isError()) {
System.out.println("OK, upload successful");
} else {
System.out.println("Error uploading, http code :" + result.getHttpStatusCode());
System.out.println("Message from server : " + result.getResponseTextNoHtml());
}
| Modifier and Type | Method and Description |
|---|---|
static HttpsFileUploaderResult |
upload(HttpsFileUploaderConfig config,
File uploadFile)
Uploads a file.
|
static HttpsFileUploaderResult |
upload(HttpsFileUploaderConfig config,
List<? extends UploadItem> uploadFiles,
Map<String,String> otherFields,
UploadProgress progressNotifier)
Uploads a file (or files) using POST method.
|
public static HttpsFileUploaderResult upload(HttpsFileUploaderConfig config, List<? extends UploadItem> uploadFiles, Map<String,String> otherFields, UploadProgress progressNotifier) throws IOException
InputStream rather than a physical file.
After the method returns the result should be examined for errors.
Multiple files can be uploaded in the same method call and there's no requirement that each file is uploaded into its own field name. Whether or not form field names for multiple files should be unique or not depends on the capabilities of the endpoint.
Some endpoints also allow uploading of other information than just the file(s). This is
supported via the otherFields argument.
This is a blocking call. You may want to spawn it off into its own thread.
See general description and example here.
config - configuration for the connection.uploadFiles - the files or streams to upload. The files will be uploaded
in the order of this list.otherFields - Other fields to be POST'ed into the form besides the file(s). Some forms allow
certain other fields, such as the name or email address of the uploader. The key of the map must
be the form field name and the value is the plain text value of the field. The null value
is acceptable.progressNotifier - Optional callback notifier. This will give a callback for each percent of the file
that has been uploaded. Use null if no progress notification is required.IOException - if the endpoint cannot be reached or if input file(s) cannot be
read.public static HttpsFileUploaderResult upload(HttpsFileUploaderConfig config, File uploadFile) throws IOException
upload(...) method.
This method only uploads a single file and expects the destination field for
the file on the server to be named "file".
After the method returns the result should be examined for errors.
config - configuration for the connection.uploadFile - file to uploadIOException - if the endpoint cannot be reached or if input file cannot be
read.upload(com.addicticks.net.httpsupload.HttpsFileUploaderConfig, java.util.List, java.util.Map, com.addicticks.net.httpsupload.UploadProgress)Copyright © 2016 Addicticks. All rights reserved.