Sample code for 30+ languages & platforms
Java

HTTPS Upload File to Web Server

See more HTTP Examples

Uploads a file to a web server using HTTPS.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The ContentType, HttpVerb, and Path properties should
        // always be explicitly set.
        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setPath("/receiveMyUpload.aspx");
        req.setContentType("multipart/form-data");

        req.addStringForUpload("fileA", "fileA.txt", "This is the contents of file A", "utf-8");
        try {
            req.addFileForUpload("starfish.jpg", "qa_data/jpg/starfish.jpg");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Http http = new Http();

        // ----------------------------------------------------------------------------
        // IMPORTANT:
        // HTTP uploads require a counterpart implementation on the server, written in any desired language
        // such as C#, Classic ASP, PHP, etc., which consumes the upload being sent.
        // See: ASP.NET Receive Upload
        // ----------------------------------------------------------------------------

        // Do the upload.
        boolean useSslTls = true;
        HttpResponse resp = new HttpResponse();
        try {
            http.httpSReq("www.example.com", 443, useSslTls, req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("response status code = " + resp.getStatusCode());
        System.out.println("response body:");
        System.out.println(resp.getBodyStr());
    }
}