HTTPS Upload library

A simple, small, no-nonsense library that allows to upload files to a remote server that accepts multipart/form-data encoded (RFC2388) uploads. The library has no external dependencies.

License

License

GroupId

GroupId

com.addicticks.oss
ArtifactId

ArtifactId

httpsupload
Last Version

Last Version

1.2.3
Release Date

Release Date

Type

Type

jar
Description

Description

HTTPS Upload library
A simple, small, no-nonsense library that allows to upload files to a remote server that accepts multipart/form-data encoded (RFC2388) uploads. The library has no external dependencies.
Project URL

Project URL

https://github.com/Addicticks/httpsupload
Project Organization

Project Organization

Addicticks
Source Code Management

Source Code Management

https://github.com/Addicticks/httpsupload.git

Download httpsupload

How to add to project

<!-- https://jarcasting.com/artifacts/com.addicticks.oss/httpsupload/ -->
<dependency>
    <groupId>com.addicticks.oss</groupId>
    <artifactId>httpsupload</artifactId>
    <version>1.2.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.addicticks.oss/httpsupload/
implementation 'com.addicticks.oss:httpsupload:1.2.3'
// https://jarcasting.com/artifacts/com.addicticks.oss/httpsupload/
implementation ("com.addicticks.oss:httpsupload:1.2.3")
'com.addicticks.oss:httpsupload:jar:1.2.3'
<dependency org="com.addicticks.oss" name="httpsupload" rev="1.2.3">
  <artifact name="httpsupload" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.addicticks.oss', module='httpsupload', version='1.2.3')
)
libraryDependencies += "com.addicticks.oss" % "httpsupload" % "1.2.3"
[com.addicticks.oss/httpsupload "1.2.3"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-core jar 1.3

Project Modules

There are no modules declared in this project.

HTTP/HTTPS Upload library

Simple and minimalist Java library which allows to upload one or several files to a remote server which accepts file uploads using HTTP POST and encoded using multipart/form-data (RFC2388).

The library is entirely based on JDK and has no external dependencies.

Features

  • Allows uploading multiple files in one operation (if supported by the endpoint).

  • Supports HTTP or HTTPS.

  • Very small and has no external dependencies, e.g. no Apache HttpClient.

  • Can upload from a physical file (the typical use case) or directly from an InputStream.

  • Certificate validation can be turned off. This is important when working against a site that uses a self-signed certificate.

  • Support for endpoints that require authentication (only authentication type "Basic" is currently supported).

  • Java's 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 infinitely large files to be uploaded without causing an out-of-memory error in the JVM. (this feature does not apply when uploading from a stream rather than physical file)

  • Support for explicitly setting network proxy. This is implemented without the use of global system properties, so can be used without affecting other classes in the same JVM.

  • Upload progress can be tracked for every 1% progress. This allows an UI to let the user know how the upload is progressing.

Motivation

We needed something small and without external dependencies.

The alternative to this minimal library is probably the Apache HttpClient. This is a much bigger library which comes in at around 1 MB including dependencies whereas our library is only 20 KB in size.

The fact is that a lot of what Apache HttpClient does is no longer required with a fairly recent Java version. There was a time when Apache HttpClient was a necessity because the standard JDK was simply too weak in the area of HTTP. This has changed with releases Java 1.4, Java 5 and Java 6 all of which introduced new features in this area.

This is not to say that Apache HttpClient isn't still superior to the standard JDK classes. It is!. It's just not enough to justify its use for a relatively simple use case as file upload is.

If our library doesn't fit your requirements you should definitely look into Apache HttpClient.

Javadoc

javadoc

Download

You can either download from here or better yet simply include it in your build via Maven Central. Maven Central

Usage

  1. Invoke static method HttpsFileUploader.upload() method.
  2. Check the method's return value.

That's it.

Here's a minimal example of the absolutely minimum scenario:

HttpsFileUploaderConfig uploaderConfig = new HttpsFileUploaderConfig(endpointURL);

// Do the upload.
// A single file is uploaded with no progress notification
result = HttpsFileUploader.upload(uploaderConfig, new File("/tmp/testfile.dat"));
 
// 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());
}

See javadoc for more information. The HttpsFileUploader class has a more elaborate code example in the javadoc.

Determining what the server endpoint accepts

In order to use the library you'll need to know a little about what the endpoint you plan to upload to actually accepts in terms of field names.

Most often the server will expose some kind of html form and the easiest way to figure out what field names are used is to peak at such a file. Suppose the endpoint's upload html file looks like this:

<html lang="en">
    <head>
        <title>File Upload</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form method="POST" action="upload" enctype="multipart/form-data" >
            File: <input type="file" name="file" id="fff" /> <br/>
            Destination: <input type="text" name="destination" value="/tmp"/>
            </br>
            <input type="submit" value="Upload" name="upload" id="upload" />
        </form>
    </body>
</html>

From this we can see the server accepts a single file to be uploaded into a field called file. In addition it also supports a plain text field called destination.

If you don't have such information available to you then there are other options:

  1. You can guess about those field names. Quite often a field of type file often has a field name of ...tada ... file. If the endpoint accepts multiple files then those names are very likely to be file1, file2, ... fileN.
  2. Ask the administrator of the endpoint for this information.
  3. Upload a file from your browser to the endpoint and use your browser's network inspector to see what is actually being sent.

Requirements

Java 7 or later.

License

Apache License, version 2.0.

com.addicticks.oss

Addicticks

Versions

Version
1.2.3
1.2.2
1.2.1
1.2.0
1.1.1
1.1.0
1.0.2
1.0.1
0.9.2