Java upload files by sending multipart request programmatically

h‮t‬tps://www.theitroad.com

Here's an example of how to upload files in Java by sending a multipart request programmatically using HttpURLConnection:

import java.io.*;
import java.net.*;

public class MultipartUploadExample {
    public static void main(String[] args) throws Exception {
        String url = "https://www.example.com/upload";
        File file1 = new File("path/to/file1");
        File file2 = new File("path/to/file2");

        // Create the connection
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // Set the request method to POST
        con.setRequestMethod("POST");

        // Set the request headers
        con.setRequestProperty("Content-Type", "multipart/form-data; boundary=---BOUNDARY");

        // Set the request body
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(("--" + "---BOUNDARY\r\n").getBytes());
        os.write(("Content-Disposition: form-data; name=\"file1\"; filename=\"" + file1.getName() + "\"\r\n").getBytes());
        os.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes());
        FileInputStream fis1 = new FileInputStream(file1);
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = fis1.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.write("\r\n".getBytes());
        fis1.close();
        os.write(("--" + "---BOUNDARY\r\n").getBytes());
        os.write(("Content-Disposition: form-data; name=\"file2\"; filename=\"" + file2.getName() + "\"\r\n").getBytes());
        os.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes());
        FileInputStream fis2 = new FileInputStream(file2);
        while ((bytesRead = fis2.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.write("\r\n".getBytes());
        fis2.close();
        os.write(("--" + "---BOUNDARY--\r\n").getBytes());
        os.flush();
        os.close();

        // Get the response code and response message
        int responseCode = con.getResponseCode();
        String responseMessage = con.getResponseMessage();
        System.out.println("Response code: " + responseCode);
        System.out.println("Response message: " + responseMessage);

        // Read the response body
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // Print the response body
        System.out.println(response.toString());
    }
}

This example creates a HttpURLConnection and sets the request method to POST. It sets the Content-Type header to indicate that the request body is a multipart form, and sets the request body to the files to be uploaded. It then reads the response body and prints it to the console. Note that you need to replace "https://www.example.com/upload" with the actual URL to which you want to upload the files, and replace "path/to/file1" and "path/to/file2" with the actual paths to the files you want to upload.